Search code examples
pyside6

pyside6 comunication between mainwindow and widget


I have 2 files (main, widget1), what i need is to be able to communicate between the 2.

edit: sorry for the mess, i´ll try to put cleaner code.

So I have a main file with a stackedWidget(with Qt designer generated Ui file)and a widget file (also with qt generated Ui file) . What i need is to be able to access the main file from the widget so i can change page of stackedWidget.

main.py:

from ui_main import Ui_MainWindow
class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.widget1 = W1.Widget1(self)
        self.ui.stackedWidget_1.insertWidget(0, self.widget1)
        self.ui.stackedWidget_1.setCurrentIndex(0)

        def nextpage():
            self.ui.stackedWidget_1.setCurrentIndex(0)

        self.ui.b0.clicked.connect(nextpage)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

widget.py

from PySide6.QtWidgets import *
from ui_widget import Ui_W1
import main
class Widget1(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)
        self.ui = Ui_W1()
        self.ui.setupUi(self)
        def widget_next_page():
            main.MainWindow().ui.stackedWidget_1.setCurrentIndex(0)
            print('0')

        self.ui.widget_button.clicked.connect(widget_next_page)

Thanks for any help, i´m trying to understand how to break the code into files so the main file don't get huge... If there is any better way(not too complicated, cause as you surely know already I'm starting.)


Solution

  • In widget_next_page you're creating a new instance of MainWindow, instead of using the existing one.

    The whole concept behind OOP modularity and Qt signals/slots is that an object should just emit a signal and not take actions on other objects on its own, especially if they are outside of its scope.

    In your case, the Widget1 should not try to change the page of its parent (the stacked widget of the main window).

    Instead, the function changing the stacked widget index should be in the main window, and that function should be connected to whatever signal it should react to. Since you already have that function (nextpage), you can reuse it.

    class MainWindow(QMainWindow):
        def __init__(self):
            # ...
            self.widget1.ui.widget_button.clicked.connect(nextpage)
    

    The above also means that you should remove the signal connection of the widget_button inside the __init__ of Widget1.

    Note that local functions should be used only when necessary, especially when connected to signals, otherwise it will be it very difficult to disconnect the specific function in case of need, since the reference is then lost when the outer function returns. Besides that, I strongly suggest you to do more research about how objects (specifically classes and instances) work in OOP in general and with Python.