如何在标签中显示图片和文字(PyQt) [英] how to show picture and text in a label (PyQt)

查看:155
本文介绍了如何在标签中显示图片和文字(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.

感谢 eyllanesc 解决了这个问题.

Thanks for eyllanesc to solve this problem.

不过,我还有两个问题.

However, I have another two questions.

发现如果在MyLable的paintEvent中显示图片和文字,喜欢:

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?

其次,当我在MyLabel的paintEvent中显示图片和文字没有super(MyLabel, self).paintEvent(QPaintEvent)时,发现只显示了文字,图片丢失了:

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)

推荐答案

覆盖 paintEvent 方法,您已移除显示 QPixmap 的行为,因此图像不可见.你应该做的是首先做 QLabelpaintEvent 方法总是做的事情,然后只绘制文本.

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 出于优化的原因,仅在图像不同时更新图像,因为它使用 QPixmapcacheKey(),所以只在需要的时候画.


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.

在第一次显示的情况下,文本被绘制,然后你设置 QPixmap 并且因为没有 QPixmap 在第一次调用时被重绘paintEvent(),它再次绘制文本,然后您再次设置QPixmap但与上一个相同,我不绘制它而是绘制那个保存在缓存中,所以在下面调用paintEvent()的时候,它只在缓存的初始图像上绘制文本.

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.

在第二种情况下,通过不使用父的paintEvent(),不使用缓存,因此不会绘制QPixmap,在这种情况下仅绘制文本.

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.

注意:不建议在 paintEvent() 方法中执行绘制以外的任务,否则可能会导致无限循环等问题.

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

这篇关于如何在标签中显示图片和文字(PyQt)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆