我的paintEvent方法并没有用PySide绘制任何东西
问题描述:
我试图在一个标签(它具有电路板的背景图像)上绘制一个圆来表示输出引脚的状态。我的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吗?
答
这里只有在代码中的语法错误,看看这个例子是如何工作的:
#!/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_())
啊,我需要设置画笔。谢谢。 – tompreston 2013-03-24 13:08:46