[PyKDE] Using custom components and Qt Designer with QWidgetFactory in PyQt - semi solution

Truls A. Tangstad kerfue+pykde at herocamp.org
Thu Nov 4 15:29:37 GMT 2004

I thought I'd just fill in anyone interested about the solution we
ended up using. It's not perfect, but a functional hack, based on
suggestions by Toby Dickenson[1] and lots of experimentation, and is
probably the way most people are doing it, but the simple solutions
are not always in plain sight.

The problem was that we needed a python QWidget-subclass embedded into
the middle of a Qt Designer created widget/dialog box, while using
QWidgetFactory to create an instance of the dialog box.

The solution:

1. Create a new custom widget in Qt Designer with class QHBox, no
   header file and Expanding as size policy in both directions. 
   (Tools->Custom->Edit Custom Widgets)
2. Create the design of the dialog in Qt Designer, inserting the QHBox
   where the python widget should be, naming the box 'holder' and store
   it to disk.

3. Create python code to match. Might look something like this:


class MyWidget(qt.QWidget):
    def __init__(self, parent):
        qt.QWidget.__init__(self, parent)

	# need to set sizepolicy to make widget fill the QHBox we're
	# putting it in.
    def paintEvent(self, event):
        # Here we do all the magic that requires us to subclass
	# QWidget.

# here we load the .ui-file and insert our widget
dialog = qt.QWidgetFactory.create('mydialog.ui', None, parent)
holder = dialog.child('holder')
# our widget is created within the QHBox and should fill it out
# completely.
my_widget = MyWidget(holder)


As Roberto comments[2], this still won't let you connect all the
signals and slots you might like to in the designer, but it makes it
easy to insert any python subclassed QWidget into designs made in Qt
Designer at runtime.

Other container widgets can probably be used instead of QHBox, but it
was the one I got the best results with, allowing my python widget to
fill it completely without having any borders or spaces or having to
create a layout.  Using QFrame instead of QHBox resulted in a big
spacing around the python widget when it was inserted into the dialog
box, which I wasn't able to remove.

In the future one should hopefully, having a
QWidgetFactory-replacement based on pyuic[2] in python, be able to
name the python-component directly in Qt Designer and even get signals
and slots connected correctly.

[1] - http://lists.kde.org/?l=pykde&m=109950310014898&w=2
[2] - http://lists.kde.org/?l=pykde&m=109956561118984&w=2
Truls A. Tangstad - <kerfue+pykde at h e r o c a m p.org>

More information about the PyQt mailing list