pythonpyside2qstackedwidget

Problem with changing QStackedWidget index


Let me describe my problem. I'm using PySide2, QtDesigner and Python. In QtDesigner i created QStakedWidget and promoted my custome pages. Im using project structure suggested here https://stackoverflow.com/a/53926554/14071081. I want to access window = MainWidget() to change currentIndex to 0 when pushButton in Add_Strategy_Page_Rule is clicked. Creating new object of MainWidget doesn't work so i thought that only way is to chatch/import somehow window from mainWindow.py. Simply importing main via import main in add_stratrgy_rule_page.py causes: screen from console Is there a solution/tric for this?

mainWindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1000</width>
    <height>600</height>
   </rect>
  </property>
  <property name="minimumSize">
   <size>
    <width>1000</width>
    <height>600</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <property name="styleSheet">
   <string notr="true"/>
  </property>
  <widget class="QWidget" name="centralwidget">
   <property name="minimumSize">
    <size>
     <width>1000</width>
     <height>600</height>
    </size>
   </property>
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <widget class="QStackedWidget" name="widget_pages">
      <property name="currentIndex">
       <number>1</number>
      </property>
      <widget class="Strategy_Widget" name="strategy_page">
       <layout class="QHBoxLayout" name="horizontalLayout_6">
        <property name="spacing">
         <number>0</number>
        </property>
        <property name="leftMargin">
         <number>0</number>
        </property>
        <property name="topMargin">
         <number>0</number>
        </property>
        <property name="rightMargin">
         <number>0</number>
        </property>
        <property name="bottomMargin">
         <number>0</number>
        </property>
       </layout>
      </widget>
      <widget class="Add_Strategy_Rule_Widget" name="add_strategy_rule_page"/>
     </widget>
    </item>
   </layout>
  </widget>
 </widget>
 <customwidgets>
  <customwidget>
   <class>Strategy_Widget</class>
   <extends>QWidget</extends>
   <header>pages/strategy_page</header>
   <container>1</container>
  </customwidget>
  <customwidget>
   <class>Add_Strategy_Rule_Widget</class>
   <extends>QWidget</extends>
   <header>pages/add_strategy_rule_page</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

mainWindow.py

import os
from PySide2 import QtWidgets
from PySide2.QtUiTools import loadUiType

current_dir = os.path.dirname(os.path.abspath(__file__))
Form, Base = loadUiType(os.path.join(current_dir, "ui/mainWindow.ui"))

class MainWidget(Base, Form):
    def __init__(self, parent=None):
        super(self.__class__, self).__init__(parent)
        self.setupUi(self)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    app.setStyle("fusion")
    window = MainWidget()
    window.show()
    sys.exit(app.exec_())

strategy_page.ui

<?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>1003</width>
    <height>666</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <property name="styleSheet">
   <string notr="true"/>
  </property>
  <layout class="QHBoxLayout" name="horizontalLayout">
   <item>
    <widget class="QLabel" name="label">
     <property name="text">
      <string>Strategy Page</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

strategy_page.py

import os
from PySide2 import QtGui
from PySide2.QtUiTools import loadUiType

current_dir = os.path.dirname(os.path.abspath(__file__))
Form, Base = loadUiType(os.path.join(current_dir, "../ui/strategy_page.ui"))


class Strategy_Widget(Base, Form):
    buy_stackedWidget_object = None
    def __init__(self, parent=None):
        super(self.__class__, self).__init__(parent)
        self.setupUi(self)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = Strategy_Widget()
    w.show()
    sys.exit(app.exec_())

add_strategy_rule_page.ui

<?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>880</width>
    <height>454</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <property name="styleSheet">
   <string notr="true"/>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <property name="spacing">
    <number>0</number>
   </property>
   <property name="sizeConstraint">
    <enum>QLayout::SetDefaultConstraint</enum>
   </property>
   <property name="leftMargin">
    <number>0</number>
   </property>
   <property name="topMargin">
    <number>0</number>
   </property>
   <property name="rightMargin">
    <number>0</number>
   </property>
   <property name="bottomMargin">
    <number>0</number>
   </property>
   <item>
    <widget class="QLabel" name="label">
     <property name="text">
      <string>Add strategy rule page</string>
     </property>
    </widget>
   </item>
   <item>
    <widget class="QPushButton" name="pushButton">
     <property name="text">
      <string>PushButton</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

add_strategy_rule_page.py

import os
from PySide2 import QtGui
from PySide2.QtUiTools import loadUiType

current_dir = os.path.dirname(os.path.abspath(__file__))
Form, Base = loadUiType(os.path.join(current_dir, "../ui/add_strategy_rule_page.ui"))


class Add_Strategy_Rule_Widget(Base, Form):
    def __init__(self, parent=None):
        super(self.__class__, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect()

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = Add_Strategy_Rule_Widget()
    w.show()
    sys.exit(app.exec_())

Solution

  • I manage to solve this problem. I have moved setCurrentIndex () to main.py and accessed it by name declared custom Widget in QStackedWIdget.

    class MainWidget(Base, Form):
        def __init__(self, parent=None):
            super(self.__class__, self).__init__(parent)
            self.setupUi(self)
            self.add_strategy_rule_page.pushButton.clicked.connect(lambda: self.changeIndex())
    
        def changeIndex(self):
            self.widget_pages.setCurrentIndex(0)
    

    Whole solution u can download from here: https://github.com/kornelHub/65428201 Problem seems easy now, but back days i didn't see that. :)