2016-12-14 294 views
0

我的代碼如下目前打開一個空白的500x500 QMainWindow。如何在PyQt4的QWidget中使用QPainter

我只是試圖用QPainter在QWidget中繪製一個圓。

這裏是我的代碼:

from PyQt4 import QtCore, QtGui, Qt 
from PyQt4.QtGui import QApplication, QMainWindow 
import sys 


class Ui_MainWindow(object): 
    def setupUi(self, MainWindow): 
     MainWindow.resize(500, 500) 
     self.centralwidget = QtGui.QWidget(MainWindow) 
     self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget) 
     MainWindow.setCentralWidget(self.centralwidget) 
     self.menubar = QtGui.QMenuBar(MainWindow) 
     self.menubar.setGeometry(QtCore.QRect(0, 0, 500, 22)) 
     MainWindow.setMenuBar(self.menubar) 
     self.statusbar = QtGui.QStatusBar(MainWindow) 
     MainWindow.setStatusBar(self.statusbar) 
     QtCore.QMetaObject.connectSlotsByName(MainWindow) 

class MyMainScreen(QMainWindow): 
    def __init__(self, parent=None): 
     QtGui.QMainWindow.__init__(self, parent) 
     self.ui = Ui_MainWindow() # This is from a python export from QtDesigner 
     self.ui.setupUi(self) 

     self.paintCircle() 


    def paintCircle(self): 
     self.painter = QtGui.QPainter(self) 
     self.painter.begin(self) 
     self.painter.setPen(Qt.QPen(Qt.QColor.black)) 
     # painter.end() 
     self.painter.drawArc(QtCore.QRectF(250, 250, 10, 10), 0, 5760) 


if __name__ == "__main__": 
    app = QApplication(sys.argv) 
    mainscreen = MyMainScreen() 
    mainscreen.show() 
    app.exec_() 

,我發現了以下錯誤:

QPainter::begin: Paint device returned engine == 0, type: 1 
QPainter::begin: Paint device returned engine == 0, type: 1 
QPainter::setPen: Painter not active 

回答

2

您必須重寫paintEvent功能,在您的情況:

from PyQt4 import QtCore, QtGui, Qt 
from PyQt4.QtGui import QApplication, QMainWindow 
import sys 


class Ui_MainWindow(object): 
    def setupUi(self, MainWindow): 
     MainWindow.resize(500, 500) 
     self.centralwidget = QtGui.QWidget(MainWindow) 
     self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget) 
     MainWindow.setCentralWidget(self.centralwidget) 
     self.menubar = QtGui.QMenuBar(MainWindow) 
     self.menubar.setGeometry(QtCore.QRect(0, 0, 500, 22)) 
     MainWindow.setMenuBar(self.menubar) 
     self.statusbar = QtGui.QStatusBar(MainWindow) 
     MainWindow.setStatusBar(self.statusbar) 
     QtCore.QMetaObject.connectSlotsByName(MainWindow) 


class MyMainScreen(QMainWindow): 
    def __init__(self, parent=None): 
     QtGui.QMainWindow.__init__(self, parent) 
     self.ui = Ui_MainWindow() # This is from a python export from QtDesigner 
     self.ui.setupUi(self) 

    def paintEvent(self, event): 
     painter = QtGui.QPainter(self) 
     painter.setPen(QtGui.QPen(QtCore.Qt.red)) 
     painter.drawArc(QtCore.QRectF(250, 250, 10, 10), 0, 5760) 

if __name__ == "__main__": 
    app = QApplication(sys.argv) 
    mainscreen = MyMainScreen() 
    mainscreen.show() 
    app.exec_() 

輸出:

enter image description here

+0

我怎樣才能讓它觸發雙擊? –

+0

雙擊,哪裏? – eyllanesc

+0

在框中,基本上我想我可以只說如果事件== QtCore.QEvent.MouseButtonDblClick,然後drawArc但事件總是QPaintEvent.paint每次顯然 –

1

你必須使用名稱

def paintEvent(self, event): 

,而不是def paintCircle(self):自動運行它何時需要。

from PyQt4 import QtCore, QtGui, Qt 
from PyQt4.QtGui import QApplication, QMainWindow 
import sys 

class Ui_MainWindow(object): 

    def setupUi(self, MainWindow): 
     MainWindow.resize(500, 500) 
     self.centralwidget = QtGui.QWidget(MainWindow) 
     self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget) 
     MainWindow.setCentralWidget(self.centralwidget) 
     self.menubar = QtGui.QMenuBar(MainWindow) 
     self.menubar.setGeometry(QtCore.QRect(0, 0, 500, 22)) 
     MainWindow.setMenuBar(self.menubar) 
     self.statusbar = QtGui.QStatusBar(MainWindow) 
     MainWindow.setStatusBar(self.statusbar) 
     QtCore.QMetaObject.connectSlotsByName(MainWindow) 

class MyMainScreen(QMainWindow): 

    def __init__(self, parent=None): 
     QtGui.QMainWindow.__init__(self, parent) 
     self.ui = Ui_MainWindow() # This is from a python export from QtDesigner 
     self.ui.setupUi(self) 

    def paintEvent(self, event): 
     self.ui.centralwidget.painter = QtGui.QPainter() 
     self.ui.centralwidget.painter.begin(self) 
     self.ui.centralwidget.painter.setPen(QtGui.QPen(QtCore.Qt.red)) 
     self.ui.centralwidget.painter.drawArc(QtCore.QRectF(250, 250, 10, 10), 0, 5760) 
     self.ui.centralwidget.painter.end() 

if __name__ == "__main__": 
    app = QApplication(sys.argv) 
    mainscreen = MyMainScreen() 
    mainscreen.show() 
    app.exec_() 

例子draw on canvas

+0

沒有必要像這樣保持對'QPainter'的引用:只需使用局部變量。 – ekhumoro

+0

@ekhumoro我使用OP代碼並做了最小的修改。 – furas