問題描述
我需要在標簽中顯示圖片和文字,這是我的代碼:
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.
發(fā)現(xiàn)如果我在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)時,發(fā)現(xiàn)只顯示文字,而且圖片不見了:
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
方法你已經(jīng)移除了顯示 QPixmap
的行為,因此圖像不可見.你應該做的是首先做 QLabel
的 paintEvent
方法總是做的,然后只繪制文本.
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
出于優(yōu)化的原因,僅在圖像不同時才更新圖像,因為它使用 QPixmap的cacheKey" rel="nofollow noreferrer">cacheKey()
,所以只在必要時繪制.
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
并且由于第一次調(diào)用時沒有重繪 QPixmap
paintEvent()
,它再次繪制文本,然后您再次設置QPixmap
,但與上一個相同,我不繪制它,而是繪制一個保存在緩存中,所以在后面調(diào)用 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()
方法中執(zhí)行繪圖以外的任務,這可能會導致類似無限循環(huán)的問題.
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)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!