from PyQt4.QtCore import QPointF, QRectF from PyQt4.QtGui import QApplication, QPainter, QPath app = QApplication([]) # create QPainter and QPath objects painter = QPainter() path = QPath() # define path path.moveTo(QPointF(10, 10)) path.lineTo(QPointF(100, 10)) path.lineTo(QPointF(100, 100)) path.lineTo(QPointF(10, 100)) path.lineTo(QPointF(10, 10)) # draw path painter.begin() painter.drawPath(path) painter.end() # display result app.exec_()
from PyQt4.QtCore import QSize, QPointF, QRectF from PyQt4.QtGui import QApplication, QMainWindow, QPainter, QPath, QWidget class CustomWidget(QWidget): def paintEvent(self, event): painter = QPainter(self) path = QPath() # define path path.moveTo(QPointF(10, 10)) path.lineTo(QPointF(100, 10)) path.lineTo(QPointF(100, 100)) path.lineTo(QPointF(10, 100)) path.lineTo(QPointF(10, 10)) # draw path painter.drawPath(path) class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() # create custom widget widget = CustomWidget() self.setCentralWidget(widget) app = QApplication([]) window = MainWindow() window.show() app.exec_()In this example, a custom widget is created by subclassing QWidget, and the paintEvent method is overridden to draw a square using QPainter's drawPath method. The widget is then added to the main window and displayed.