2013-03-23 74 views
1

我試圖在一個標籤(它具有電路板的背景圖像)上繪製一個圓來表示輸出引腳的狀態。我的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

這裏只有在代碼中的語法錯誤,看看這個例子是如何工作的:

#!/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_()) 
+0

啊,我需要設置畫筆。謝謝。 – tompreston 2013-03-24 13:08:46

相關問題