我的paintEvent方法在PySide中没能绘制任何内容
我正在尝试在一个标签上画一个圆圈,这个标签的背景是电路板的图片,用来表示一个输出引脚的状态。
目前我只是想画点东西,但我什么都没画出来。
这是我简化过的类:
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 个回答
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_())