Page switching by QStackedWidget
Solved
Qt for Python
-
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!
```
code_textfrom 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><html><head/><body><p>Next butt</p><p><br/></p></body></html></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>
-
Hi and welcome to devnet,
Your QStackedWidget is named "pages" and you should use that object to switch between the widgets inside them.
-
Hi and welcome to devnet,
Your QStackedWidget is named "pages" and you should use that object to switch between the widgets inside them.
@SGaist So i changed the name of in the code to pages, but still dont know how i should connect button with this command
-
Might be a silly question but are you sure you are clicking the button you connected ?
-
@SGaist Okay, i found my mistake, thanks for help!