Issue
Using PySide (Qt) I'm trying to open a non-modal window from a modal window.
The modal window actually waits for a mandatory answer, there's an icon on it that pops up an help window about what the user could answer.
I cannot find the way to close the (non-modal) help window before the modal parent actually is closed.
Is there a way to set the child window as modal while still keep its parent modal?
Solution
In the example below I show you how to close another window from a modal dialog before or after the modal dialog is closed. It is not very difficult and only requires to call close
on the other window (your help window).
Example:
from PySide import QtCore, QtGui
def start_modal_dialog():
modal_dlg = QtGui.QDialog(main_window) # a modal dialog
modal_dlg.setWindowTitle('Modal Dialog')
modal_dlg.setFixedSize(200, 200)
modeless_help_window = QtGui.QLabel('Explanations', modal_dlg, QtCore.Qt.Window) # a modeless help window
modeless_help_window.setWindowTitle('Modeless Help Window')
modeless_help_window.setFixedSize(200, 200)
modeless_help_window.show()
help_window_close_button = QtGui.QPushButton('Close Help window', modal_dlg)
help_window_close_button.clicked.connect(modeless_help_window.close)
modal_dlg.exec_() # execute the dialog
modeless_help_window.close() # close the modeless help window after the modal dialog window is closed
app = QtGui.QApplication([]) # create app
main_window = QtGui.QMainWindow() # main window
main_window.setWindowTitle('Main Window')
main_window.setFixedSize(200, 200)
dlg_start_button = QtGui.QPushButton('Start Modal dialog', main_window) # add a button
dlg_start_button.clicked.connect(start_modal_dialog) # which shows the dialog
main_window.show()
app.exec_()
I have a main window with a button. When the button is clicked a modal dialog (instance of QDialog
) is started but at the same time a modeless window (for simplicity just a QLabel
) is shown. In the modal dialog there is also a button and if it is clicked the modeless window is closed. Also after execution of the dialog the modeless window is closed (just in case the button was not pressed).
There you see that closing the other window only requires calling close
, but I also set up a hierachy. The modal dialog is a child of the main window and the modeless (help) window is a child of the modal dialog. Without it, it may not work as well.
Answered By - Trilarion
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.