我的paintEvent方法没有用PySide绘制任何东西

时间:2013-03-23 19:57:15

标签: python qt pyqt pyside qpainter

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

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

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

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吗?

1 个答案:

答案 0 :(得分: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_())
相关问题