0

LAYOUT IMAGe

For reference purpose consider the above example.

On the left I have two buttons PAGE1 and PAGE2 and on the right I have a QStackedWidget consisting of two widgets.

I made two seperate UI files with names page1.ui and page2.ui enter image description here

I used "Promote Widget" to add page1.ui and page2.ui inside my stacked widget. (See the image for reference)

The following is structure of my files:

├── main.py ├── pages │ ├── page1.py │ ├── page2.py └── ui ├── main.ui ├── page1.ui ├── page2.ui

What I would like to achieve is that I am able to change the Index of Stacked Widget from a Widget inside Stacked Widget.

Currently I am only able to change Index of Stacked Widget by using buttons in main.ui.

CODE: main.py

import os
from PyQt5 import QtGui, uic, QtWidgets
from functools import partial


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

class MainWidget(Base, Form):
    def __init__(self, parent=None):
        super(self.__class__, self).__init__(parent)
        self.setupUi(self)
        buttons = (self.page1Button, self.page2Button)
        for i, button in enumerate(buttons):
            button.clicked.connect(partial(self.stackedWidget.setCurrentIndex, i))



if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication.instance()
    if app is None:
        app = QtWidgets.QApplication(sys.argv)

    app.setStyle("fusion")
    w = MainWidget()
    w.show()
    sys.exit(app.exec_())

page1.py:

import os
from PyQt5 import QtGui, uic, QtWidgets

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

class page1Window(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)
    w = page1Window()
    w.show()
    sys.exit(app.exec_())

page2.py is similar to page1.py

Suppose I have a button in page1.ui, how do I use this button to change the index of Stacked Widget using this button?

If you feel I am missing anything or have doubt on what I exactly want to do please leave a comment.

manjy
  • 109
  • 1
  • 2
  • 12

1 Answers1

0

Okay I managed to get it working. Turns out the solution was pretty straight forward

Here is what I did: main.py:

import os
from PyQt5 import QtGui, uic, QtWidgets
from functools import partial


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

class MainWidget(Base, Form):
    def __init__(self, parent=None):
        super(self.__class__, self).__init__(parent)
        self.setupUi(self)
        buttons = (self.page1Button, self.page2Button)
        for i, button in enumerate(buttons):
            button.clicked.connect(partial(self.stackedWidget.setCurrentIndex, i))


        self.page_1.pushButton.clicked.connect(self.foo)
        #the above line calls foo() inside main.py when a button on page_1 is clicked



    def foo(self):
        # set stack widget to what ever you need
        self.stackedWidget.setCurrentIndex(1)


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication.instance()
    if app is None:
        app = QtWidgets.QApplication(sys.argv)

    app.setStyle("fusion")
    w = MainWidget()
    w.show()
    sys.exit(app.exec_())
manjy
  • 109
  • 1
  • 2
  • 12