要将图像显示到 QWidget 上,可以使用 QPainter 类的 drawPixmap 方法。以下是一个简单的示例代码:

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QPainter
from PyQt5.QtWidgets import QWidget, QApplication

class MyWidget(QWidget):
    def __init__(self):
        super().__init__()

        # 创建一个 QPixmap 对象
        self.image = QPixmap('image.jpg')

    def paintEvent(self, event):
        painter = QPainter(self)
        # 将图像绘制到 QWidget 上
        painter.drawPixmap(self.rect(), self.image)

if __name__ == '__main__':
    app = QApplication([])
    widget = MyWidget()
    widget.show()
    app.exec_()

在这个例子中,我们创建了一个 QPixmap 对象,并在 paintEvent 事件中使用 QPainter 的 drawPixmap 方法将其绘制到 QWidget 上。使用 self.rect() 来指定绘制的区域为 QWidget 的整个区域。最后,我们启动了一个 QApplication 并显示了这个 QWidget。

在 PyQt5 中将图像显示到 QWidget 上

原文地址: https://www.cveoy.top/t/topic/nlHi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录