0

Good afternoon. This is an MRE of the problem that you can easily run in your machine

import sys

from PyQt5.QtWidgets import (
    QWidget, QMenuBar, QMenu, QApplication,
    QAction, QMainWindow,
    QVBoxLayout, QLabel
)


class mainwindow(QMainWindow):

    def __init__(self, screen2, screen3):
        super().__init__()

        self._screen2 = screen2
        self._screen3 = screen3

        self.tela()
        self.acoes()
        self.sinais()

    def tela(self):

        layout = QVBoxLayout()
        label = QLabel('screen 1')
        layout.addWidget(label)
        widget_central = QWidget()
        self.setCentralWidget(widget_central)
        widget_central.setLayout(layout)

    def acoes(self):

        self.action_screen2 = QAction('screen 2')
        self.action_screen3 = QAction('screen 3')

        menu_opt = QMenuBar(self)
        self.setMenuBar(menu_opt)
        menu = QMenu("avaliable screens", self)
        menu_opt.addMenu(menu)
        screen_menu = menu.addMenu("screens")
        screen_menu.addAction(self.action_screen2)
        screen_menu.addAction(self.action_screen3)

    def sinais(self):

        self.action_screen2.triggered.connect(self.tela2)
        self.action_screen3.triggered.connect(self.tela3)

    def tela2(self):

        self.setCentralWidget(self._screen2)

    def tela3(self):

        self.setCentralWidget(self._screen3)


class widget1(QWidget):

    def __init__(self):
        super().__init__()

        self.screen()

    def screen(self):
        layout = QVBoxLayout()
        label = QLabel('screen 2')
        layout.addWidget(label)
        self.setLayout(layout)


class widget2(QWidget):

    def __init__(self):
        super().__init__()

        self.screen()

    def screen(self):
        layout = QVBoxLayout()
        label = QLabel('screen 3')
        layout.addWidget(label)
        self.setLayout(layout)


def main():

    applic = QApplication(sys.argv)
    screen2 = widget1()
    screen3 = widget2()
    app = mainwindow(screen2, screen3)
    app.show()
    sys.exit(applic.exec_())


if __name__ == '__main__':
    main()

If you run this code you'll be presented with a menu which allows you to choose from different screen options, in this sample the screens are made of a label that reads the number of the chosen screen, I'm using QActions to make the connections. Screen 1 is default, screens 2 and 3 are represented by 2 classes that inherit from QWidget and what "glue" things together is the setCentralWidget() function of the QMainWindow class.

This works for the first few changes, like: from screen1 (default) to screen2 to 3. But this only work once. Let's say that you selected screen 2 then 3, then if you want to go back to screen 2 (and vice-versa) the application will eventually crash.

What can I do to prevent the application to close itself if I want to go back to an already selected screen?

zeca_abc
  • 41
  • 5
  • This behaviour is [as per the docs](https://doc.qt.io/qt-5/qmainwindow.html#setCentralWidget): "Note: QMainWindow takes ownership of the widget pointer and ***deletes it at the appropriate time***". Use a [QStackedWidget](https://doc.qt.io/qt-5/qstackedwidget.html) instead. – ekhumoro Dec 06 '21 at 19:18
  • Use a [QStackedWidget](https://doc.qt.io/qt-5/qstackedwidget.html). – musicamante Dec 06 '21 at 20:48
  • My dear friends @musicamante and ekhumoro, every time I make a question here I learn something new. Your answers were exactly what I was looking for, your help was much appreciated. – zeca_abc Dec 07 '21 at 00:01
  • musicamante and @ekhumoro Thank you both very much!! Have a good night – zeca_abc Dec 07 '21 at 00:01
  • @zeca_abc you're welcome. Since it's confirmed that your question is a duplicate, please accept the proposal in order to mark it as such. – musicamante Dec 07 '21 at 00:02
  • I'll do this right away!! – zeca_abc Dec 07 '21 at 00:04

0 Answers0