pyqt4: trouble making custom widget appear in mainwindow

Phil Thompson phil at riverbankcomputing.com
Sun May 4 11:04:04 EDT 2008


On Sunday 04 May 2008, Lance Gamet wrote:
> Hi,
> about 3 years ago I did a bit of Qt3 and 4 on C++, now I am trying to do
> something in python.
>
> A QMainWindow subclass as my mainwindow. Its basically a frame with no
> widgets of its own.
>
> Several QWidget subclasses each representing a mode of the program, these
> widgets should appear in the mainwindow depending on what part of the
> program is being used.
>
> I have made the main window and a couple of widgets with designer-qt4 and
> generate a Ui_Whatever.py with pyuic4 and subclass that.
>
> My problem is I cant get my widgets to appear in the main window.
>
> My MainWindow.py looks like (imports etc skipped):
>
> class MainWindow(QMainWindow, Ui_MainWindow):
>     def __init___(self):
>         QMainWindow.__init__(self)
>         self.setupUi(self)
>
>     def newTeamWizardMode(self):
>         Widget = QWidget()
>         self.newTeamWizard = CreateNewTeamWizard(Widget)
>         self.newTeamWizard.setGeometry(QRect(0,0,90, 160))
>         self.newTeamWizard.setObjectName("newTeamWizard")
>         self.newTeamWizard.show()
>
> My CreateNewTeamWizard is defined like so:
>
> class CreateNewTeamWizard(QWidget, Ui_CreateNewTeamWizard):
>     def __init___(self, parent = None):
>         QWidget.__init__(self, parent)
>         self.setupUi(self)
>
> In my main program i make the main window and try and add the widget like
> this:
>
>     app = Qt.QApplication(sys.argv)
>     mainWindow = MainWindow()
>     app.connect(app, Qt.SIGNAL("lastWindowClosed()"), app, Qt.SLOT("quit
> ()"))
>     mainWindow.newTeamWizardMode()
>     mainWindow.show()
>
> One thing i tried was changing the CreateNewTeamWizard in
> MainWindow.newTeamWizardMode to CreateNewTeamWizard(Widget, self) to
> provide the main window as the parent but I get the following error:
>
>     self.newTeamWizard = CreateNewTeamWizard(Widget, self)
> TypeError: argument 2 of QWidget() has an invalid type
>
> I have looked for examples, but everything I have found is a bit too
> simple. Creating a single window or widget, displaying it as the programs
> main window and exiting when the window is closed.
>
> I havent found anything to tell me how to make a widget appear in a
> mainwindow how I want.
>
> Any tips?

I think QMainWindow.setCentralWidget() is what you are looking for.

> If someone can point me to a nice open source program that uses pyqt4
> that might help.

The eric4 IDE probably covers most things you'll ever need.

Phil



More information about the Python-list mailing list