[PyQt] derive from a designer widget

Andreas Pakulat apaku at gmx.de
Fri Mar 13 14:59:10 GMT 2009


On 13.03.09 12:18:24, Mario Daniel Carugno wrote:
> 2009/3/13, Andreas Pakulat <apaku at gmx.de>:
> >  > I want that it could be as simple as derivating the 'data' class
> >  > from the 'main window' class. I've read that derivating a widget
> >  > from another one, makes that widget to appear inside it's parent,
> >  > right ?
> >
> > No thats wrong. Deriving one class from another is something different than
> >  adding a widget to a parent widget.
> 
> What about this ?
> 
> #!/bin/env python
> import sys
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
> 
> class Frame(QDialog):
>    def __init__(self):
>       QDialog.__init__(self)
>       pass
> 
> class GUI(Frame):
>    def __init__(self):
>       Frame.__init__(self)
>       b = QLabel("HOLA")
>       l = QVBoxLayout()
>       l.addWidget(b)
>       self.setLayout(l)
> 
> if __name__ == "__main__":
>    app = QApplication(sys.argv)
>    ui = GUI()
>    ui.show()
>    app.exec_()
> 
> This snippet creates a Frame and a widget as a subclass of it.

No its not, you're mixing different terms here. A subclass (aka derived
class) in python is created by this statement:

class Foo(Bar):
...

In this case Foo is a subclass of Bar.

However in the above example (with the magic re-parenting of
QLayout&QWidget) b is just a child widget inside your Frame.

> This is enough to make the widget appear into the Frame
> I want to achieve that, but with Frame being a compound widget with its
> central area, and that area being the default 'container' to hold
> derived widgets.

Then you should do similar to what QMainWindow does, provide a
setCentralWidget() method which takes a complete widget and uses for the
central area of the window.

> I note that one problem with this using designer, is that designer creates
> widgets subclassing 'object' instead of 'QWidget' or 'QDialog'. With the
> main form derived from 'object' i can't make the above subclassing example.

Thats because Qt designer doesn't design any widgets. You design a template
of how your gui should look like and then you can apply that template to a
given QWidget. That will create the designed gui inside the given QWidget.
Afterwards you can add that QWidget into any layout and the GUI will show
up inside that layout.

Maybe you should have a closer look at the PyQt examples and maybe check
out Mark Summerfields book on PyQt. It seems your missing some of the
basics of Qt GUI's.

Andreas

-- 
You will pioneer the first Martian colony.


More information about the PyQt mailing list