Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Switching between multiple pages
Forum Updated to NodeBB v4.3 + New Features

Switching between multiple pages

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 3.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LT-K101
    wrote on last edited by
    #1

    Hi guys, I saw this code online and I'm confused what the pages.ui line is? Can anyone please explain that line to me please? Because the code is switching between just two windows and the pages.ui seem to be a third ui window.Thanks in advance.

    from PyQt5 import uic
    from PyQt5.QtWidgets import QApplication, QMainWindow
    
    from first import First
    from second import Second
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
            uic.loadUi('pages.ui', self)
            self.first = First()
            self.stackedWidget.addWidget(self.first)
            self.first.change_btn.clicked.connect(self.go_to_second)
            self.second = Second()
            self.stackedWidget.addWidget(self.second)
            self.second.change_btn.clicked.connect(self.go_to_first)
    
        def go_to_first(self):
            self.stackedWidget.setCurrentIndex(0)
    
        def go_to_second(self):
            self.stackedWidget.setCurrentIndex(1)
    
    
    if __name__ == '__main__':
        app = QApplication([])
        window = MainWindow()
        window.show()
        app.exec_()
    

    first.py

    from PyQt5 import uic
    from PyQt5.QtWidgets import QWidget
    
    class First(QWidget):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            uic.loadUi('first.ui', self
    

    second.py

    from PyQt5 import uic
    from PyQt5.QtWidgets import QWidget
    
    class Second(QWidget):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            uic.loadUi('second.ui', self
    
    JonBJ 1 Reply Last reply
    0
    • L LT-K101

      Hi guys, I saw this code online and I'm confused what the pages.ui line is? Can anyone please explain that line to me please? Because the code is switching between just two windows and the pages.ui seem to be a third ui window.Thanks in advance.

      from PyQt5 import uic
      from PyQt5.QtWidgets import QApplication, QMainWindow
      
      from first import First
      from second import Second
      
      class MainWindow(QMainWindow):
          def __init__(self):
              super().__init__()
              uic.loadUi('pages.ui', self)
              self.first = First()
              self.stackedWidget.addWidget(self.first)
              self.first.change_btn.clicked.connect(self.go_to_second)
              self.second = Second()
              self.stackedWidget.addWidget(self.second)
              self.second.change_btn.clicked.connect(self.go_to_first)
      
          def go_to_first(self):
              self.stackedWidget.setCurrentIndex(0)
      
          def go_to_second(self):
              self.stackedWidget.setCurrentIndex(1)
      
      
      if __name__ == '__main__':
          app = QApplication([])
          window = MainWindow()
          window.show()
          app.exec_()
      

      first.py

      from PyQt5 import uic
      from PyQt5.QtWidgets import QWidget
      
      class First(QWidget):
          def __init__(self, *args, **kwargs):
              super().__init__(*args, **kwargs)
              uic.loadUi('first.ui', self
      

      second.py

      from PyQt5 import uic
      from PyQt5.QtWidgets import QWidget
      
      class Second(QWidget):
          def __init__(self, *args, **kwargs):
              super().__init__(*args, **kwargs)
              uic.loadUi('second.ui', self
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @LT-K101
      The pages.ui is the designer file just for the QStackedWidget (self.stackedWidget), e.g. see https://www.tutorialspoint.com/pyqt/pyqt_qstackedwidget.htm? That is the "container" widget which holds references to the individual page widgets and allows switching between which (single) one of them is shown at any time.

      L 1 Reply Last reply
      2
      • JonBJ JonB

        @LT-K101
        The pages.ui is the designer file just for the QStackedWidget (self.stackedWidget), e.g. see https://www.tutorialspoint.com/pyqt/pyqt_qstackedwidget.htm? That is the "container" widget which holds references to the individual page widgets and allows switching between which (single) one of them is shown at any time.

        L Offline
        L Offline
        LT-K101
        wrote on last edited by
        #3

        @JonB Thanks a lot for your response, Its clear now.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved