Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I switch layouts in a window using PyQt?? (Without closing/opening windows)

Tags:

python

qt

pyqt4

I am currently attempting to create a program using python and PyQt4 (not Qt Designer). I created a login class (QDialog) and a Homepage class (QMainWindow). However, because my program will consist of loads of pages (the navigation through the program will be large) i wanted to know how to switch layouts in QMainWindow rather than constantly creating new windows and closing old ones. For example, i would have the MainWindow ('HomePage') layout set as the default screen once logged in and would then have a subclass within MainWindow which allows me to navigate to user settings (or any other page). Instead of creating a new window and closing MainWindow, is there a way for me to swap the MainWindow layout to the User Setting layout?? (apologies if this doesnt make sense, im new to PyQt). An example code is shown below (V.Basic code)

----------------------------------------------------------------------------------

import sys     
from PyQt4.QtGui import *      
from PyQt4.QtCore import *     

class MainWindow(QMainWindow):
    #Constructor
    def __init__(self):
        super(MainWindow, self).__init__() #call super class constructor
        
        button1 = QPushButton("User Settings", self)
        button1.clicked.connect(UserSelection)
        button1.resize(50,50)
        button1.move(350,50)

        self.show()

class UserSelection(?):
    ...

def main():
   app = QApplication(sys.argv) #Create new application
   Main = MainWindow()
   sys.exit(app.exec_()) #Monitor application for events

if __name__ == "__main__":
    main()
like image 415
Hamzah Akhtar Avatar asked Mar 27 '14 19:03

Hamzah Akhtar


People also ask

How can I switch to another screen without opening a new window in PyQt5?

Create a main_UI with this structure: QMainWindow > QFrame (centralWidget) > QLayout > another_q_frame (this frame will contain both of your main pages) Now create 2 other QWidget (lets call them screen_1 & screen_2 ) windows containing one button each (this button would be used to switch between the screens)

What IDE should I use for PyQt?

Wing Pro is a Python IDE that can be used to develop, test, and debug Python code written for the PyQt cross-platform GUI development toolkit.


1 Answers

from PyQt4 import QtCore, QtGui

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.central_widget = QtGui.QStackedWidget()
        self.setCentralWidget(self.central_widget)
        login_widget = LoginWidget(self)
        login_widget.button.clicked.connect(self.login)
        self.central_widget.addWidget(login_widget)
    def login(self):
        logged_in_widget = LoggedWidget(self)
        self.central_widget.addWidget(logged_in_widget)
        self.central_widget.setCurrentWidget(logged_in_widget)


class LoginWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(LoginWidget, self).__init__(parent)
        layout = QtGui.QHBoxLayout()
        self.button = QtGui.QPushButton('Login')
        layout.addWidget(self.button)
        self.setLayout(layout)
        # you might want to do self.button.click.connect(self.parent().login) here


class LoggedWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(LoggedWidget, self).__init__(parent)
        layout = QtGui.QHBoxLayout()
        self.label = QtGui.QLabel('logged in!')
        layout.addWidget(self.label)
        self.setLayout(layout)



if __name__ == '__main__':
    app = QtGui.QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()
like image 61
ivica Avatar answered Sep 23 '22 22:09

ivica