Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Page switching by QStackedWidget
Forum Updated to NodeBB v4.3 + New Features

Page switching by QStackedWidget

Scheduled Pinned Locked Moved Solved Qt for Python
qt for python
5 Posts 2 Posters 1.1k Views 2 Watching
  • 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.
  • M Offline
    M Offline
    Marek Bilinski
    wrote on last edited by Marek Bilinski
    #1

    I have the problem. Im new to QT and i try to make simple app. I have to switch beetwen pages but, when i try to make it work my code doesnt!screen.png ```
    code_text

    from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QStackedWidget
    from PySide6.QtUiTools import QUiLoader
    from PySide6.QtCore import Slot
    class MainWindow(QMainWindow):
        def __init__(self, parent=None):
            super().__init__(parent)
    
            loader = QUiLoader() #ładowanie kodu strony do pythona
            self.window = loader.load("home.ui", self)
            self.window.setFixedSize(500, 400)
            self.show()  # wykonywanie
            #łaczenie sygnałów
            self.window.sub_butt.clicked.connect(self.func)
            #self.window.nxt_butt_1.clicked.event()
            #self.window.nxt_butt_1.clicked.connect(self.func_2())
            #self.nxt_butt_1.clicked.connect(self.stackedWidget.setCurrentIndex(self.stackedWidget.currentIndex() + 1))
        @Slot()
        def func(self):
             print(self.window.Name.text(),"\n",self.window.Name_2.text(),"\n",self.window.Name_3.text(),"\n",self.window.Name_4.text())
        def func_2(self):
            self.QStackedWidget.setCurrentIndex(self.QStackedWidget.currentIndex() + 1)
    if __name__ == "__main__":
        app=QApplication(sys.argv)
        win=MainWindow()
    
        win.setWindowTitle("Project")
        sys.exit(app.exec())
    
    **and my ui code**
    
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>Form</class>
     <widget class="QWidget" name="Form">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>499</width>
        <height>390</height>
       </rect>
      </property>
      <property name="contextMenuPolicy">
       <enum>Qt::DefaultContextMenu</enum>
      </property>
      <property name="windowTitle">
       <string>Form</string>
      </property>
      <layout class="QVBoxLayout" name="verticalLayout_4">
       <item>
        <widget class="QStackedWidget" name="pages">
         <property name="maximumSize">
          <size>
           <width>481</width>
           <height>16777215</height>
          </size>
         </property>
         <property name="contextMenuPolicy">
          <enum>Qt::CustomContextMenu</enum>
         </property>
         <property name="currentIndex">
          <number>0</number>
         </property>
         <widget class="QWidget" name="page_1">
          <property name="enabled">
           <bool>true</bool>
          </property>
          <property name="minimumSize">
           <size>
            <width>0</width>
            <height>372</height>
           </size>
          </property>
          <layout class="QVBoxLayout" name="verticalLayout_9">
           <item>
            <widget class="QLabel" name="label">
             <property name="maximumSize">
              <size>
               <width>16777215</width>
               <height>50</height>
              </size>
             </property>
             <property name="text">
              <string>Enter your name</string>
             </property>
             <property name="alignment">
              <set>Qt::AlignCenter</set>
             </property>
            </widget>
           </item>
           <item>
            <layout class="QVBoxLayout" name="verticalLayout_3">
             <item>
              <widget class="QLineEdit" name="Name">
               <property name="text">
                <string/>
               </property>
               <property name="alignment">
                <set>Qt::AlignCenter</set>
               </property>
               <property name="placeholderText">
                <string>Enter your name </string>
               </property>
               <property name="clearButtonEnabled">
                <bool>true</bool>
               </property>
              </widget>
             </item>
            </layout>
           </item>
           <item>
            <layout class="QHBoxLayout" name="horizontalLayout">
             <item>
              <widget class="QPushButton" name="bck_butt_1">
               <property name="text">
                <string>Back</string>
               </property>
              </widget>
             </item>
             <item>
              <widget class="QPushButton" name="nxt_butt_1">
               <property name="text">
                <string>Next</string>
               </property>
               <property name="text">
                <string>Next</string>
               </property>
              </widget>
             </item>
            </layout>
           </item>
          </layout>
         </widget>
         <widget class="QWidget" name="page_2">
          <property name="contextMenuPolicy">
           <enum>Qt::CustomContextMenu</enum>
          </property>
          <layout class="QVBoxLayout" name="verticalLayout_2">
           <item>
            <widget class="QLabel" name="label_2">
             <property name="maximumSize">
              <size>
               <width>16777215</width>
               <height>50</height>
              </size>
             </property>
             <property name="text">
              <string>Enter your Surname</string>
             </property>
             <property name="alignment">
              <set>Qt::AlignCenter</set>
             </property>
            </widget>
           </item>
           <item>
            <layout class="QVBoxLayout" name="verticalLayout_5">
             <item>
              <widget class="QLineEdit" name="Name_2">
               <property name="text">
                <string/>
               </property>
               <property name="alignment">
                <set>Qt::AlignCenter</set>
               </property>
               <property name="placeholderText">
                <string>Enter your surname </string>
               </property>
               <property name="clearButtonEnabled">
                <bool>true</bool>
               </property>
              </widget>
             </item>
            </layout>
           </item>
           <item>
            <layout class="QHBoxLayout" name="horizontalLayout_2">
             <item>
              <widget class="QPushButton" name="bck_butt_2">
               <property name="text">
                <string>Back</string>
               </property>
              </widget>
             </item>
             <item>
              <widget class="QPushButton" name="nxt_butt_2">
               <property name="whatsThis">
                <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Next butt&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
               </property>
               <property name="text">
                <string>Next</string>
               </property>
              </widget>
             </item>
            </layout>
           </item>
          </layout>
         </widget>
         <widget class="QWidget" name="page_3">
          <layout class="QVBoxLayout" name="verticalLayout">
           <item>
            <widget class="QLabel" name="label_3">
             <property name="maximumSize">
              <size>
               <width>16777215</width>
               <height>50</height>
              </size>
             </property>
             <property name="text">
              <string>Enter your adress</string>
             </property>
             <property name="alignment">
              <set>Qt::AlignCenter</set>
             </property>
            </widget>
           </item>
           <item>
            <layout class="QVBoxLayout" name="verticalLayout_6">
             <item>
              <widget class="QLineEdit" name="Name_3">
               <property name="text">
                <string/>
               </property>
               <property name="alignment">
                <set>Qt::AlignCenter</set>
               </property>
               <property name="placeholderText">
                <string>Enter your adress</string>
               </property>
               <property name="clearButtonEnabled">
                <bool>true</bool>
               </property>
              </widget>
             </item>
            </layout>
           </item>
           <item>
            <layout class="QHBoxLayout" name="horizontalLayout_3">
             <item>
              <widget class="QPushButton" name="bck_butt_3">
               <property name="text">
                <string>Back</string>
               </property>
              </widget>
             </item>
             <item>
              <widget class="QPushButton" name="nxt_butt_3">
               <property name="text">
                <string>Next</string>
               </property>
              </widget>
             </item>
            </layout>
           </item>
          </layout>
         </widget>
         <widget class="QWidget" name="page_4">
          <layout class="QVBoxLayout" name="verticalLayout_8">
           <item>
            <widget class="QLabel" name="label_4">
             <property name="maximumSize">
              <size>
               <width>16777215</width>
               <height>50</height>
              </size>
             </property>
             <property name="text">
              <string>Enter your phone number</string>
             </property>
             <property name="alignment">
              <set>Qt::AlignCenter</set>
             </property>
            </widget>
           </item>
           <item>
            <layout class="QVBoxLayout" name="verticalLayout_7">
             <item>
              <widget class="QLineEdit" name="Name_4">
               <property name="text">
                <string/>
               </property>
               <property name="alignment">
                <set>Qt::AlignCenter</set>
               </property>
               <property name="placeholderText">
                <string>Enter your phone number</string>
               </property>
               <property name="clearButtonEnabled">
                <bool>true</bool>
               </property>
              </widget>
             </item>
            </layout>
           </item>
           <item>
            <layout class="QHBoxLayout" name="horizontalLayout_4">
             <item>
              <widget class="QPushButton" name="bck_butt_4">
               <property name="text">
                <string>Back</string>
               </property>
              </widget>
             </item>
             <item>
              <widget class="QPushButton" name="nxt_butt_4">
               <property name="text">
                <string>Next</string>
               </property>
              </widget>
             </item>
            </layout>
           </item>
          </layout>
         </widget>
         <widget class="QWidget" name="page_5">
          <layout class="QVBoxLayout" name="verticalLayout_12">
           <item>
            <widget class="QPushButton" name="sub_butt">
             <property name="text">
              <string>Submit</string>
             </property>
            </widget>
           </item>
           <item>
            <layout class="QVBoxLayout" name="verticalLayout_11">
             <item>
              <widget class="QLabel" name="odp">
               <property name="text">
                <string/>
               </property>
               <property name="textFormat">
                <enum>Qt::AutoText</enum>
               </property>
               <property name="alignment">
                <set>Qt::AlignCenter</set>
               </property>
              </widget>
             </item>
            </layout>
           </item>
          </layout>
         </widget>
        </widget>
       </item>
      </layout>
     </widget>
     <tabstops>
      <tabstop>Name</tabstop>
      <tabstop>nxt_butt_1</tabstop>
      <tabstop>bck_butt_1</tabstop>
      <tabstop>bck_butt_2</tabstop>
      <tabstop>nxt_butt_2</tabstop>
      <tabstop>Name_2</tabstop>
      <tabstop>bck_butt_3</tabstop>
      <tabstop>nxt_butt_3</tabstop>
      <tabstop>Name_3</tabstop>
      <tabstop>bck_butt_4</tabstop>
      <tabstop>nxt_butt_4</tabstop>
      <tabstop>Name_4</tabstop>
     </tabstops>
     <resources/>
     <connections/>
    </ui>
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Your QStackedWidget is named "pages" and you should use that object to switch between the widgets inside them.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Your QStackedWidget is named "pages" and you should use that object to switch between the widgets inside them.

        M Offline
        M Offline
        Marek Bilinski
        wrote on last edited by
        #3

        @SGaist So i changed the name of in the code to pages, but still dont know how i should connect button with this command

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Might be a silly question but are you sure you are clicking the button you connected ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          0
          • SGaistS SGaist

            Might be a silly question but are you sure you are clicking the button you connected ?

            M Offline
            M Offline
            Marek Bilinski
            wrote on last edited by
            #5

            @SGaist Okay, i found my mistake, thanks for help!

            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