[PySide] Sub classing question

Phil phil_lor at bigpond.com
Sat Jun 29 03:36:42 CEST 2013


Thank you for reading this,

This is my first posting so I hope the amount of code that follows is 
not a problem.

I'd like to display a frame on my main window and draw a red line on 
that frame. Just for the exercise, I'd also like to draw a yellow line 
on the main window. I have been able to do both but not at the same time.

The code that follows is my latest attempt. As it stands, the frame with 
it's red line is being displayed but only the frame is displayed without 
the main window.

This is the main window;

from mainwindow import Ui_MainWindow
import sys
from PySide import QtCore, QtGui

class DrawTest(QtGui.QMainWindow):
     def __init__(self, parent=None):
         super(DrawTest, self).__init__(parent)
         self.ui = Ui_MainWindow()
         self.ui.setupUi(self)

     def buttonClicked(self):
         print("clicked")

     def paintEvent(self, event):
         qp = QtGui.QPainter()
         qp.begin(self)
         pen = QtGui.QPen(QtCore.Qt.yellow, 4)
         qp.setPen(pen)
         self.drawLine(event, qp)
         qp.end()

     def drawLine(self, event, qp):
         qp.drawLine(10, 10, 30, 30)


This is the frame class;

from mainwindow import Ui_MainWindow
from draw_test import DrawTest
from PySide import QtCore, QtGui

class Frame(QtGui.QFrame):
     def __init__(self, parent=None):  # I tried Ui_MainWindow here
         super(DrawTest, self).__init__(parent)
         self.frame = Frame(self)
         self.setCentralWidget(self.frame)

     def paintEvent(self, event):
         qp = QtGui.QPainter()
         qp.begin(self)
         pen =QtGui.QPen(QtCore.Qt.red, 4)
         qp.setPen(pen)
         self.drawLine(event, qp)
         qp.end()

     def drawLine(self, event, qp):
         qp.drawLine(10, 10, 30, 30)

And this is the main module;

#from mainwindow import Ui_MainWindow
from draw_test import DrawTest
from frame import Frame
from PySide import QtCore, QtGui
import sys

if __name__ == "__main__":
     app = QtGui.QApplication(sys.argv)
     mySW = DrawTest()
     mySW.show()
     sys.exit(app.exec_())

-- 
Regards,
Phil



More information about the PySide mailing list