如何在pyqt qdock widget中添加图片

How to add an image in pyqt qdock widget

我正在尝试使用 pyqt 创建图像查看器。我正在尝试的是创建一个 qdockwidget 来显示图像。我创建了 qPixmap objecte 并尝试将其添加为 qdockwidget 中的小部件。

from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import (QApplication, QMainWindow)
from PyQt5.QtCore import Qt

from reportlab.graphics.charts.piecharts import Pie
from reportlab.graphics.shapes import *

from PIL import ImageQt

from reportlab.graphics import renderPM

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(800, 600)

        dockWidget = QtWidgets.QDockWidget()
        dockWidget.setWindowTitle("Image Viewer")

        d = Drawing(200, 100)
        pc = Pie()
        pc.x = 65
        pc.y = 0
        pc.width = 100
        pc.height = 100
        pc.data = [10, 20, 30, 40, 50, 60]
        pc.labels = ['a', 'b', 'c', 'd', 'e', 'f']
        pc.slices.strokeWidth = 0.5
        pc.slices[3].popout = 10
        pc.slices[3].strokeWidth = 2
        pc.slices[3].strokeDashArray = [2, 2]
        pc.slices[3].labelRadius = 1.75
        pc.slices[3].fontColor = colors.red
        d.add(pc)

        test = renderPM.drawToPILP(d)

        QtImage1 = ImageQt.ImageQt(test)
        QtImage2 = QtGui.QImage(QtImage1)
        pixmap = QtGui.QPixmap.fromImage(QtImage2)
        label = QtWidgets.QLabel('testing', self)
        label.setPixmap(pixmap)

        #dockWidget.setWidget(pixmap)
        dockWidget.setFloating(False)
        self.addDockWidget(Qt.RightDockWidgetArea, dockWidget)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWidget = MainWindow()
    myWidget.show()

    sys.exit(app.exec_())

QPixmap不是一个视觉元素,而是一个对象,它的图像信息经过优化,可以在widgets中呈现,所以不能直接添加到QDockWidget中,但必须使用允许它显示的widget , 比如 QLabel:

dockWidget.setWidget(label)