[PyQt] Drawing on a QFrame

Phil Thompson phil at riverbankcomputing.com
Fri Jun 28 09:56:50 BST 2013


On Fri, 28 Jun 2013 15:35:20 +1000, Phil <phil_lor at bigpond.com> wrote:
> Thank you for reading this.
> 
> I'd like to draw on a QFrame.
> 
> The following does draw a line but not on the frame, instead the line is

> drawn on the main window.
> 
> I've done this years ago with Qt and C++ but I don't remember how I did
it.
> 
> class DrawTest(QtGui.QMainWindow):
>      def __init__(self, parent=None):
>          super(DrawTest, self).__init__(parent)
>          self.ui = Ui_MainWindow()
>          self.ui.setupUi(self)
>          frame = QtGui.QFrame()
> 
>      def paintEvent(frame, event):
>          qp = QtGui.QPainter()
>          qp.begin(frame)
>          pen = QtGui.QPen(QtCore.Qt.yellow, 4)
>          qp.setPen(pen)
>          frame.drawLine(event, qp)
>          qp.end()
> 
>      def drawLine(frame, event, qp):
>          qp.drawLine(10, 10, 30, 30)

Sub-class the frame and reimplement its paintEvent(). Also make sure the
frame isn't being garbage collected (like it is above) and add it to the
GUI somewhere.

Phil


More information about the PyQt mailing list