user3777
user3777

Reputation: 41

PyQT4 - Replacing widgets on main window

I`m new in PyQT4 and I've faced the problem below.

Some application must collect users' data on one screen and show next screen on button click.

main.py

app = QtGui.QApplication(sys.argv)
#here I create main window with inherited class
w = SAN_MainWindow()
#and apply some basic geometry
w.SetUI()
#here first screen rendered and button event applyed
w.ApplyWidget( SAN_Intro() )
w.show()
sys.exit(app.exec_())

Rendering of first widget occurs correctly.

SAN_MainWindow.py:

class SAN_MainWindow(QtGui.QMainWindow):
    def __init__(self ):
        super(SAN_MainWindow, self).__init__()

    def SetUI(self):
        self.setObjectName(_fromUtf8("MainWindow"))
        self.resize(789, 602)
        self.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)) 
        self.central_widget = QtGui.QWidget(self)   
        self.central_widget.setObjectName(_fromUtf8("central_widget"))
        self.setCentralWidget(self.central_widget)

    def ApplyWidget( self, widget ):
        self.active_widget = widget
        self.active_widget.Setup(self.central_widget)
        self.active_widget.SetControls( self )

    def RemoveActiveWidget( self ):
        self.active_widget.widget().setParent(None)

    def ShowInstruction( self ):
        self.RemoveActiveWidget()
        self.ApplyWidget( SAN_Instruction() )
        #self.centralWidget().update() <- Problem

SAN_Intro.py:

class SAN_Intro(object):
    def __init__(self):
        super(SAN_Intro, self).__init__()

    def Setup(self, widget):
        self.gridLayoutWidget = QtGui.QWidget(widget)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(80, 30, 638, 451))
        self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
        self.gridLayout = QtGui.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(-1, 16, 16, -1)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        #a lot of form inputs instructions....
    def SetControls( self, main_window ):
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL("clicked()"), main_window.ShowInstruction)

    def widget(self):
        return self.gridLayoutWidget

SAN_Instruction is almost the same as SAN_Intro, but with different commands in Setup:

class SAN_Instruction(object):

    def __init__(self):
        super(SAN_Instruction, self).__init__()

    def Setup(self, widget):
        self.verticalLayoutWidget = QtGui.QWidget(widget)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(40, 20, 591, 521))
        self.verticalLayoutWidget.setObjectName(_fromUtf8("verticalLayoutWidget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setMargin(0)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        #a lot of form inputs instructions.... 

    def SetControls( self, main_window ):
        pass

    def widget(self):
        return self.verticalLayoutWidget

AND HERE IS QUESTION: How must I show the second (generated on fly with SAN_Instruction) widget? I tried to update/show/repaint mainwindow/centralwidget/parent. But all I've got on button click is self.RemoveActiveWidget().

Maybe my concept is incorrect?

Thanx in advance

Upvotes: 2

Views: 1602

Answers (2)

HaTiMuX
HaTiMuX

Reputation: 641

You can use a "QtGui.QStackedWidget()". You Split your Window to many Frames, add each frame to the QStackedWidget() and use function setCurrentWidget() to choose which frame to display (switch between frames).

self.central_widget.addWidget(self.mainFrame)
self.central_widget.setCurrentWidget(self.mainFrame)

Upvotes: 0

jonathan.hepp
jonathan.hepp

Reputation: 1643

Well, your code isn't complete but i would advise you to take advantage of the hide() method. You can define this in your button function, so that all the widgets you do not want to appear get hidden, and on the same function call the widgets you want to appear on their place. I use to do that way because it makes my projects more organized and easy to track and debug. Furthermore, this also allows you to make that widgets visible again if you need, with the show() method.

Upvotes: 2

Related Questions