Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
708 views
in Technique[技术] by (71.8m points)

python - how to show picture and text in a label (PyQt)

I need to show a picture and text in a label, and this is my code:

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class MyLabel(QLabel):
    def __init__(self):
        super(MyLabel, self).__init__()

    def paintEvent(self, QPaintEvent):
        pos = QPoint(50, 50)
        painter = QPainter(self)
        painter.drawText(pos, 'hello,world')
        painter.setPen(QColor(255, 255, 255))

class Window(QWidget):
    def __init__(self):
        super(Window, self).__init__()
        layout = QHBoxLayout(self)
        self.label = MyLabel()
        self.pixmap = QPixmap('icon.png')
        self.label.setPixmap(self.pixmap)

        layout.addWidget(self.label)


if __name__ == '__main__':

    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

The label only display the text, and the picture is missing. How to display both image and text in the label.

Thanks for eyllanesc to solve this problem.

However, I have another two questions.

I found that if I display the image and text in the paintEvent of MyLable, likes:

def paintEvent(self, QPaintEvent):
    super(MyLabel, self).paintEvent(QPaintEvent)

    pos = QPoint(50, 50)
    painter = QPainter(self)
    painter.drawText(pos, 'hello,world')
    painter.setPen(QColor(255, 255, 255))

    self.pixmap = QPixmap('C:\Users\zhq\Desktop\DicomTool\icon.png')
    self.setPixmap(self.pixmap)

The text was display over the image even I firstly display the text and then display the image. Why?

Second, when I display the image and text in the paintEvent of MyLabel without the super(MyLabel, self).paintEvent(QPaintEvent), I found only the text is shown, and the picture is missing:

def paintEvent(self, QPaintEvent):
    pos = QPoint(50, 50)
    painter = QPainter(self)
    painter.drawText(pos, 'hello,world')
    painter.setPen(QColor(255, 255, 255))

    self.pixmap = QPixmap('C:\Users\zhq\Desktop\DicomTool\icon.png')
    self.setPixmap(self.pixmap)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Overwriting the paintEvent method you have removed the behavior of displaying the QPixmap so the image is not visible. What you should do is first do what the paintEvent method of QLabel always does and then just paint the text.

class MyLabel(QLabel):
    def __init__(self):
        super(MyLabel, self).__init__()

    def paintEvent(self, event):
        super(MyLabel, self).paintEvent(event)
        pos = QPoint(50, 50)
        painter = QPainter(self)
        painter.drawText(pos, 'hello,world')
        painter.setPen(QColor(255, 255, 255))

QLabel for reasons of optimization only updates the image if it is different for it uses the cacheKey() of QPixmap, so only draw when necessary.

In your first case the first time it is displayed, the text is painted, then you set the QPixmap and since no QPixmap is redrawn for the first time it calls paintEvent(), it draws the text again, then you set the QPixmap again but being the same as the previous one, I do not draw it but draw the one that is saved in the cache, so in the following times that paintEvent() is called, it only draws the text on the initial image of the cache.

In the second case, by not using the paintEvent() of the parent, the cache is not used, so the QPixmap will not be drawn and in that case only the text is drawn.

Note: it is not advisable to do a task other than drawing in the paintEvent() method, you could cause problems like an infinite loop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...