使用 QPainter 和 paintEvent 在 PYQT5 中的 QLabel 中包含的 Pixmap 上绘制圆圈

Posted

技术标签:

【中文标题】使用 QPainter 和 paintEvent 在 PYQT5 中的 QLabel 中包含的 Pixmap 上绘制圆圈【英文标题】:Using QPainter and paintEvent to draw circles on a Pixmap that is contained in a QLabel in PYQT5 【发布时间】:2018-09-14 00:45:44 【问题描述】:

我是 PYQT5 的新手,我想使用以下代码在 PYQT5 的 MainWindow UI 中的 QLabel 中包含的 Pixmap 上绘制一个圆圈:

from PyQt5 import QtCore, QtGui, QtWidgets

background_image_path = '001_01_01_041_05.png'

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(800, 600)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.label = QtWidgets.QLabel(self.centralwidget)
    self.label.setGeometry(QtCore.QRect(60, 60, 331, 401))
    self.label.setObjectName("label")
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 28))
    self.menubar.setObjectName("menubar")
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)

    self._image = QtGui.QPixmap(background_image_path)

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    self.label.setPixmap(self._image)

def paintEvent(self, event):
    painter = QtGui.QPainter(self._image)
    painter.drawPixmap(self.rect(), self._image)
    pen = QtGui.QPen()
    pen.setWidth(5)
    painter.setPen(pen)
    painter.drawEllipse(300, 300, 70, 70)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

但是,我无法调用 paintEvent(self, event) 函数。当我调用paintEvent(self, event) 时,我看不到画在像素图上的圆圈。我尝试将 QPainter 设置为 Pixmap 本身(在painter = QtGui.QPainter(self._image) 中),但这也不起作用。

我做错了什么?

下面的代码有效,这是我的代码所基于的:

from PyQt5 import QtWidgets, QtGui, QtCore
background_image_path = '001_01_01_041_05.png'

class ImageScroller(QtWidgets.QWidget):
    def __init__(self):


def paintEvent(self, event):
    painter = QtGui.QPainter(self)
    painter.drawPixmap(self.rect(), self._image)
    pen = QtGui.QPen()
    pen.setWidth(5)
    painter.setPen(pen)
    painter.drawEllipse(300, 300, 70, 70)


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = ImageScroller()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

任何想法或建议将不胜感激!

【问题讨论】:

【参考方案1】:

你错过了几行。 试试看:

from PyQt5 import QtWidgets, QtGui, QtCore

background_image_path = 'E:/_Qt/img/heart.png'     # '001_01_01_041_05.png'

class ImageScroller(QtWidgets.QWidget):                    
    def __init__(self):
        super().__init__()                                   # <-------

        self._image = QtGui.QPixmap(background_image_path)   # <-------

    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        painter.drawPixmap(self.rect(), self._image)
        pen = QtGui.QPen()
        pen.setWidth(5)
        painter.setPen(pen)
        painter.drawEllipse(300, 300, 70, 70)


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = ImageScroller()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

这里真正的问题是你正在编辑pyuic生成的模块, 这总是一个非常糟糕的主意。试试看:

from PyQt5 import QtCore, QtGui, QtWidgets

background_image_path = 'E:/_Qt/img/heart.png'

class Ui_MainWindow(object):          
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(200, 0, 760, 560))
        self.label.setObjectName("label")

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 28))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

#        self._image = QtGui.QPixmap(background_image_path)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
#        self.label.setPixmap(self._image)
        self.label.setText(_translate("MainWindow", 
                           """
                           It is not recommended to modify the design file, 
                           it is appropriate to create another file 
                           to join the logic with the design.
                           """))



#    def paintEvent(self, event):
#        painter = QtGui.QPainter(self._image)
#        painter.drawPixmap(self.rect(), self._image)
#        pen = QtGui.QPen()
#        pen.setWidth(5)
#        painter.setPen(pen)
#        painter.drawEllipse(300, 300, 70, 70)


class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self):
        super(MyApp, self).__init__()

        self.setupUi(self)

        self._image = QtGui.QPixmap(background_image_path)

    def paintEvent(self, event):
        painter = QtGui.QPainter(self)  
        painter.drawPixmap(self.rect(), self._image)

        #pen = QtGui.QPen()
        #pen.setWidth(5)
        #painter.setPen(pen)
        painter.setPen(QtGui.QPen(QtCore.Qt.blue, 5))
        painter.drawEllipse(350, 350, 70, 70)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)

#    MainWindow = QtWidgets.QMainWindow()
#    ui = Ui_MainWindow()
#    ui.setupUi(MainWindow)
#    MainWindow.show()
    window = MyApp()
    window.show() 

    sys.exit(app.exec_())

【讨论】:

抱歉,我忘记将这些行添加到该函数中。但是我已经知道下面的代码可以工作,我只是不知道如何让上面代码中带有 MainWindow 的 UI 工作。我正在尝试让 paintEvent(self, event) 在 Ui_MainWindow 类上工作。 我添加了一些建议,试试看。

以上是关于使用 QPainter 和 paintEvent 在 PYQT5 中的 QLabel 中包含的 Pixmap 上绘制圆圈的主要内容,如果未能解决你的问题,请参考以下文章

为啥 QPainter 可能会拒绝在paintEvent 中工作?

为啥当 QPushButton::paintEvent 后面跟着 QPainter::fillRect 时应用程序会崩溃?

paintEvent中的Qt QPainter消除了用paintGL绘制的图形

Qt学习之路(24): QPainter(改写paintEvent)

如何使用 Qt QPainter 绘制线性渐变弧?

Qt - 创建 QPainter