Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Facing problems with switching through multiple windows in my GUI



  • Hello all,

    I am designing a multiple window GUI using Qt Designer, I have read in pretty much everywhere that it's not good practice to edit the code generated by pyuic5, and instead create a separate logic file that imports the windows into my subclasses. I did just that, and when I tried to move from the first to second window it works just fine, but for some reason I can't move from second to third window. However, if I am to start from the second window, it would move to the third just fine.

    I believe my mistakes have to do with OOP, as i am still new to it, but I can't figure out what is it exactly.
    So, will anyone please direct me into the way where I can solve this problem?
    Thank you.

    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtCore import QTimer, Qt
    
    from First import Ui_MainWindow
    from Second import Ui_Form 
    from Third import Ui_Form2
    
    
    class FirstWindow (QtWidgets.QMainWindow, Ui_MainWindow):
        def __init__(self, parent=None):
            super(FirstWindow, self).__init__(parent)
            self.setupUi(self)
            
            #----------------------------------------------------------------------
            self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
            QTimer.singleShot (5000, self.secondscreen)
            #----------------------------------------------------------------------
        
        @QtCore.pyqtSlot()    
        def secondscreen(self):
            self.Form = QtWidgets.QWidget()
            self.ui = Ui_Form()
            self.ui.setupUi(self.Form)
            self.Form.showMaximized() 
            self.close()
            
            
    class SecondWindow (QtWidgets.QWidget, Ui_Form):
        def __init__(self, parent=None):
            super(SecondWindow, self).__init__(parent)
            self.setupUi(self)
    
         
            #----------------------------------------------------------------------
            self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
            self.pushButton.clicked.connect(self.thirdscreen)
            #----------------------------------------------------------------------
            
        @QtCore.pyqtSlot()    
        def thirdscreen(self):
            self.Form2 = QtWidgets.QWidget()
            self.ui = Ui_Form2()
            self.ui.setupUi(self.Form2)
            self.Form2.showMaximized() 
            self.close()
            
            
    class ThirdWindow (QtWidgets.QWidget, Ui_Form2):
        def __init__(self, parent=None):
            super(ThirdWindow, self).__init__(parent)
            self.setupUi(self)
            
            
    
            
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        Software = FirstWindow()
        Software.show()
        sys.exit(app.exec_())
    

  • Lifetime Qt Champion

    Then it looks like you should rather use a QStackedWidget rather than re-create the widgets all the time and handle the switching at a higher level.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you doing some kind of wizard ?

    Or do you want to go back and forth with them ?



  • Hello, @SGaist, thank you for your reply.
    No, It's not a wizard. I should be able to go back and forth.


  • Lifetime Qt Champion

    Then it looks like you should rather use a QStackedWidget rather than re-create the widgets all the time and handle the switching at a higher level.



  • @SGaist You're absolutely right. The transition is much smoother now.

    One more question please, my first window is a loading screen like the one shown in the picture attached, and then the software windows themselves are in the QStackedWidget.

    In many other software, while the loading screen is showing, the software icon doesn't actually appear in the taskbar, instead it only appears once the actual software has started (The QStackedWidget in my case).
    Do you happen to know how to hide the icon from the taskbar while showing my loading screen (which is a QMainWindow btw) and then showing it later on?
    Thanks a lot.

    icepak-launch-workbench.3ea599da.png


  • Lifetime Qt Champion



  • I think loading screen should be separated from other windows.
    Are you aware that there is a QSplashScreen class?
    https://doc.qt.io/qt-5/qsplashscreen.html



  • Hey @Bonnie, I did not know of this class. I started reading about it, and I believe that this would be better for my software. Here's another useful example for PyQt5 if someone is interested:
    https://stackoverflow.com/questions/58661539/create-splash-screen-in-pyqt5

    Quick question though, I am planing on building my loading screen (QSplashScreen) on MainWindow1 and then have the rest of my windows in QStackedWidget built on MainWindow2. I have read in an article that using multiple Mainwindows in your software is bad practice. Is this correct? or I can just go forward with my plan?



  • @Ahmed-Zeid said in Facing problems with switching through multiple windows in my GUI:

    using multiple Mainwindows in your software is bad practice. Is this correct?

    In my opinion, yes.
    Don't put QSplashScreen in any window. Itself is a window, just without title bar / window frame / taskbar item.



  • @Ahmed-Zeid
    You should only need one main window. (Splash screen is outside of main window, as @Bonnie said.)

    and then have the rest of my windows in QStackedWidget built on MainWindow2

    What do you mean by "built on"? Your main window should have a QStackedWidget as its main/central widget, and then your various "pages" should just be QWidgets placed in the stacked widget, not QMainWindows themselves. The main window's framework is around the outside of the stacked widget.



  • Hello, @JonB, thanks a lot for your reply. Yes, this is exactly what I meant.
    Sorry for being unclear before.


Log in to reply