[PyQt] derive from a designer widget

Andreas Pakulat apaku at gmx.de
Thu Mar 12 07:53:30 GMT 2009


On 11.03.09 19:29:17, Mario Daniel Carugno wrote:
> 2009/3/11 Andreas Pakulat <apaku at gmx.de>:
> > On 11.03.09 16:55:11, Mario Daniel Carugno wrote:
> >> Hi, i'm using pytq4 + designer4.
> >> I want to design a generic main window with common buttons (next,
> >> previous, new, edit, ...)
> >> with a central area for data. That central area could be a frame.
> >> In QT3 remember i could derive my widget from another one made with
> >> designer, but that is
> >> not possible in QT4. That the behaviour i want.
> >
> > You can still do the same thing, except that you'll additionally need a
> > python class subclassing QMainWindow that loads the ui from the designer
> > form. Then you can create subclasses from that and set the central
> > widget. If the form is a member of your base class you can also access
> > it and all widgets from the subclass.
> >
> Thank you Andreas, i'll try it. Just in case, do you have some example
> of that ?

Its pretty easy (from the top of my head, so might not work right away, but
you should get the idea)

class MyMainWindow(QMainWindow):
    def __init__(self,parent):
        QMainWindow.__init__(self,parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(this)

class MyCustomMainWindow(MyMainWindow):
    def __init__(self,parent):
        MyMainWindow.__init__(self,parent)
        self.mybutton = QPushButton(self.ui.centralFrame)


That should create a new pushbutton inside centralFrame of your form, if
that frame has a layout attached to it when using the MyCustomMainWindow
class. If you want something else in the central frame you can write up a
new class in a similar manner.

Andreas    

-- 
Never look up when dragons fly overhead.


More information about the PyQt mailing list