1

我正在尝试在标签(具有电路板的背景图像)上画一个圆圈来表示输出引脚的状态。

我现在只是想画一些东西,但我没有画出任何东西。

这是我的(缩短的)课程:

class MyClass(QMainWindow, Ui_myGeneratedClassFromQtDesigner):
    def paintEvent(self, event):                                                                                                                     
        super(QMainWindow, self).paintEvent(event)                             
        print("paint event")
        painter = QtGui.QPainter()
        painter.begin(self)
        painter.drawElipse(10, 10, 5, 5)
        painter.end()

paint event打印到控制台,但窗口中没有绘制任何内容。我正确使用 QPainter 吗?

4

1 回答 1

1

你的代码中只有一个语法错误,看看这个例子是如何工作的:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4 import QtGui, QtCore

class MyWindow(QtGui.QLabel):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

    def animate(self):
        animation = QtCore.QPropertyAnimation(self, "size", self)
        animation.setDuration(3333)
        animation.setStartValue(QtCore.QSize(self.width(), self.height()))
        animation.setEndValue(QtCore.QSize(333, 333))
        animation.start()

    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        painter.setBrush(QtGui.QBrush(QtCore.Qt.red))
        painter.drawEllipse(0, 0, self.width() - 1, self.height() - 1)
        painter.end()

    def sizeHint(self):
        return QtCore.QSize(111, 111)

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.show()
    main.animate()

    sys.exit(app.exec_())
于 2013-03-24T12:13:44.110 回答