0

I have an application, in which at one point of execution a dialog box is opened, waiting for the user to respond and according to his response the application acts.

This dialog box I need to be a QMainwindow.

I know that if I do it with a QDialog it fixes it, but I would like to know if there is a way to do it with QMainwindow.

I have made a test scenario, WINDOW 2 is QMainWindow in a Modal modality and the print order is executed even if it has not been closed and WINDOW 3 is QDialog and the print is not executed until the window has not been closed.

import sys
from PyQt6 import QtCore, QtWidgets

class Ui_FirstWindow(object):
    def setupUi(self, FirstWindow):
        FirstWindow.resize(400, 300)
        self.centralWidget = QtWidgets.QWidget(FirstWindow)
        self.pushButton = QtWidgets.QPushButton(self.centralWidget)
        self.pushButton.setGeometry(QtCore.QRect(110, 130, 191, 23))
        self.pushButton.setText("Load Second Window")
        self.pushButton2 = QtWidgets.QPushButton(self.centralWidget)
        self.pushButton2.setGeometry(QtCore.QRect(110, 170, 191, 23))
        self.pushButton2.setText("LoadThirdWindow")
        FirstWindow.setCentralWidget(self.centralWidget)

    def LoadSecondWindow(self):
        SecondWindow = QtWidgets.QMainWindow()
        ui = Ui_SecondWindow()
        ui.setupUi(SecondWindow)
        SecondWindow.show()
        
class Ui_SecondWindow(object):
    
    def setupUi(self, SecondWindow):
        SecondWindow.resize(400, 300)
        self.centralWidget = QtWidgets.QWidget(SecondWindow)
        self.pushButton = QtWidgets.QPushButton(self.centralWidget)
        self.pushButton.setGeometry(QtCore.QRect(110, 130, 191, 23))
        SecondWindow.setCentralWidget(self.centralWidget)
        self.pushButton.setText('PushButton')
        
class Controller:

    def __init__(self):
        pass

    def Show_FirstWindow(self):

        self.FirstWindow = QtWidgets.QMainWindow()
        self.ui = Ui_FirstWindow()
        self.ui.setupUi(self.FirstWindow)
        self.ui.pushButton.clicked.connect(self.Show_SecondWindow)
        self.ui.pushButton2.clicked.connect(self.Show_ThirdWindow)
        
        self.FirstWindow.show()

    def Show_SecondWindow(self):
        
        self.SecondWindow = QtWidgets.QMainWindow()
        self.ui = Ui_SecondWindow()
        self.ui.setupUi(self.SecondWindow)
       
        self.SecondWindow.setWindowModality(QtCore.Qt.WindowModality.ApplicationModal)
        self.SecondWindow.show()
        print("Don't Stop")
        
    def Show_ThirdWindow(self):
        self.ThirdWindow = QtWidgets.QDialog()
        self.ThirdWindow.exec()
        print('Yess Stop')

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    Controller = Controller()
    Controller.Show_FirstWindow()
    sys.exit(app.exec())
RBenet
  • 171
  • 1
  • 10
  • Why do you need a QMainWindow? – musicamante Sep 01 '22 at 10:10
  • Because I need some of the features of QMainWindow, such as: the status bar, Dock Windows,... – RBenet Sep 01 '22 at 10:14
  • You could create a QEventLoop that behaves similarly to that of the dialog (which is what QDialog does when calling `exec()`), but that might actually make things unnecessarily complex, especially if the widget has complex elements. If you need to do something when the window closes, create a QMainWindow subclass with a custom signal and emit it in the override of the `closeEvent()`, then connect that signal to whatever you need. You should also not edit/merge pyuic generated code, by the way. – musicamante Sep 01 '22 at 10:44
  • Thank you musicamente, I improve my test code following your comments and I published here https://stackoverflow.com/questions/50029201/wait-until-qtwidgets-qmainwindow-is-closed-before-continuing/73571379#73571379 – RBenet Sep 01 '22 at 14:52

0 Answers0