[PyKDE] QT4 PyQt Wowes - a bug ?

David Boddie david at boddie.org.uk
Fri Sep 29 17:58:20 BST 2006


On Friday 29 September 2006 11:44:36 +0100, Dave S wrote:

> Now knowing that PyQt4 does not support the QT4  'Qt3Support' module - and
> having opened and re-saved my QT3 dialogs as QT4 dialogs in QT4 designer I
> realised that the dialogs are simply changed to use the ' Qt3Support'
> module  - I hit a problem !

Yes, that can be a problem. Think of it as an incentive to use the new
features in Qt 4. ;-)

> So I decided that I would re-make the dialogs in QT4 designer afresh :)
> Being carefull to not use any QT3 options I re-made my main window.
> 
> On my XP box I could not find pyuic4 so I 
> used /python24/lib/site-packages/PyQt4/uic/pyuic.py ...

Do you have any of the PyQt4 tools in the /python24/bin directory?

> I am assuming that this is the right thing to do ?

I don't think it matters that much if you use that file directly.

> On execution I get an attribute error 'setcentralwidget' - to make sure it
> was not something I was doing I made another simple dialog in QT4 designer
> - run it and it works OK
> 
> I am stuck - I have looked at the output of pyuic and have googled to see 
> if 'setCentralWidget' is supported under PyQt4 - I looked in QDialog & 
> QWidget but cannot find a reference.

OK. The problem is that setCentralWidget() is a QMainWindow method.

From your .ui file, I can see that you've created a main window (based on the
QMainWindow class) from the following lines:

[...]
>  <class>MainWindow</class>
>  <widget class="QMainWindow" name="MainWindow" >
[...]

The code generated by pyuic4 is designed to be used with an instance of the
appropriate class, either by calling the setupUi() method with an instance
or by subclassing the appropriate class and "mixing in" the generated class.
(See http://doc.trolltech.com/4.1/designer-using-a-component.html for more
information about each of these methods.)

In your code, you create an instance of the generated user interface class,
Ui_MainWindow, which contains a call to setCentralWidget(). However, you are
passing a QDialog instance to its setupUi() method, and this fails because
QDialog doesn't have a setCentralWidget() method.

> def main(args):
> 
>     app = QtGui.QApplication(sys.argv)
>     window = QtGui.QDialog()

[...]

>     ui = mainscreen.Ui_MainWindow()
>     ui.setupUi(window)

This leads to the following:

>   File "E:\Documents and 
> Settings\User\Desktop\PxQxAuditor\dialogs\mainscreen.py", line 80, in
> setupUi 
>     MainWindow.setCentralWidget(self.centralwidget)
> AttributeError: setCentralWidget
> >>> 

What you meant to do was to create an instance of QMainWindow and pass that
to setupUi(), as in the following code:

def main(args):

    app = QtGui.QApplication(sys.argv)
    window = QtGui.QMainWindow()

    ui = mainscreen.Ui_MainWindow()
    ui.setupUi(window)
    
    window.show()
    sys.exit(app.exec_())

Hope this helps,

David




More information about the PyQt mailing list