Linking Python to multiple qt forms

trzewiczek trzewiczek at trzewiczek.info
Tue Jan 12 00:54:54 CET 2010


On 01/11/2010 11:57 PM, Zabin wrote:
> Hey!
>
> I am new to PyQt programming and am trying to activate a secondary qt
> form from a primary form. I am unsure as to how this will work as i am
> using pyuic4 to convert .ui file to .py and then importing it by:
>
> from SimLCM_GUI import Ui_main
>
> I am unsure as to how the self.ui....commands will function if there
> are two such .ui files.
>
> Thanks in advance for any advice!
>
> Cheers
> Zabin
>    
The easiest way for me was to create three classes - two for each form 
created in QDesigner and one that holds them as objects. If you want to 
switch from one to another you can emit a SIGNAL to the form's SLOT 
setVisible(bool) to hide it  and another to the other to show it. My 
code looks sth like that:

#
# imports

from options_ui import Ui_options
from main_ui import Ui_main


#
# class for the first ui form

class OptionsPanel(QtGui.QWidget):

     def __init__(self, parent = None):
         QtGui.QWidget.__init__(self, parent)

         self.ui = Ui_options()
         self.ui.setupUi(self)


#
# class for the second ui form

class MainPanel(QtGui.QWidget):

     def __init__(self, parent = None):
         QtGui.QWidget.__init__(self, parent)

         self.ui = Ui_main()
         self.ui.setupUi(self)


#
# holder class for the panels

class MyApp(QtGui.QWidget):

     def __init__(self, parent = None):
         QtGui.QWidget.__init__(self, parent)

         self.setWindowTitle("My App")

         self.options_panel = OptionsPanel(self)
         self.main_panel = MainPanel(self)

         QtCore.QObject.connect(self, QtCore.SIGNAL("main(bool)"), 
self.main_panel, QtCore.SLOT("setVisible(bool)"))
         QtCore.QObject.connect(self, QtCore.SIGNAL("options(bool)"), 
self.options_panel, QtCore.SLOT("setVisible(bool)"))

         self.showMain()


     #
     # functions that toggle the views

     def showOptions(self):
         self.emit(QtCore.SIGNAL("main(bool)"), False)
         self.emit(QtCore.SIGNAL("options(bool)"), True)

     def showMain(self):
         self.emit(QtCore.SIGNAL("options(bool)"), False)
         self.emit(QtCore.SIGNAL("main(bool)"), True)


As I said - that works for me. If there's a better way, being a new to 
PyQt4 I'm looking for it as well!

Best from Poland,
trzewiczek



More information about the Python-list mailing list