Skip to content Skip to sidebar Skip to footer

How To Use Qpainter In A Qwidget In Pyqt4

My code below currently opens a 500x500 QMainWindow that is blank. I am simply trying to draw a circle in the QWidget using QPainter. Here is my code: from PyQt4 import QtCore, QtG

Solution 1:

You must override the paintEvent function, in your case:

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


classUi_MainWindow(object):
    defsetupUi(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)


classMyMainScreen(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)

    defpaintEvent(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_()

Output:

enter image description here

Solution 2:

You have to use name

defpaintEvent(self, event):

instead of def paintCircle(self): to run it automatically when it is needed.

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

classUi_MainWindow(object):

    defsetupUi(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)

classMyMainScreen(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)

    defpaintEvent(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_()

Examples draw on canvas

Post a Comment for "How To Use Qpainter In A Qwidget In Pyqt4"