更改 QStackedWidget 索引的问题
Problem with changing QStackedWidget index
让我描述一下我的问题。我正在使用 PySide2、QtDesigner 和 Python。在 QtDesigner 中,我创建了 QStakedWidget 并提升了我的 custome pages. Im using project structure suggested here 。
我想访问 window = MainWidget()
以在单击 Add_Strategy_Page_Rule 中的按钮时将 currentIndex 更改为 0。 创建 MainWidget 的新对象不起作用,所以我认为只有这样是 chatch/import 以某种方式从 mainWindow.py window
。在 add_stratrgy_rule_page.py 中简单地通过 import main
导入 main 会导致:screen from console
这个有solution/tric吗?
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_())
我设法解决了这个问题。我已将 setCurrentIndex () 移动到 main.py 并通过 QStackedWIdget.
中的 name declared custom Widget 访问它
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)
您可以从这里下载完整的解决方案:https://github.com/kornelHub/65428201
问题现在看起来很简单,但过去我没有看到。 :)
让我描述一下我的问题。我正在使用 PySide2、QtDesigner 和 Python。在 QtDesigner 中,我创建了 QStakedWidget 并提升了我的 custome pages. Im using project structure suggested here window = MainWidget()
以在单击 Add_Strategy_Page_Rule 中的按钮时将 currentIndex 更改为 0。 创建 MainWidget 的新对象不起作用,所以我认为只有这样是 chatch/import 以某种方式从 mainWindow.py window
。在 add_stratrgy_rule_page.py 中简单地通过 import main
导入 main 会导致:screen from console
这个有solution/tric吗?
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_())
我设法解决了这个问题。我已将 setCurrentIndex () 移动到 main.py 并通过 QStackedWIdget.
中的 name declared custom Widget 访问它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)
您可以从这里下载完整的解决方案:https://github.com/kornelHub/65428201 问题现在看起来很简单,但过去我没有看到。 :)