How to change QDialog modality at runtime?

I have a QDialog, and I read a lot about the differences between show (), exec (), and open (). Unfortunately, I did not find a solution to change the modality of the dialog at runtime. I have an application, and from there my QDialog starts. I have a toggle button in this dialog box, and when I click on it, QDialog must change the modality so that it can interact with the application - but this should not happen all the time - only when checking the toggle button.

Is there a possibility? I could not solve the problem with setting setModal (true / false), it just allows me to launch it modally, switch the button and set it to non-modal, but then I can not return to modal.

Here is the code:

Dialog launch:

from the main window:

_dialog = new ToggleModalDialog(this, id, this); _dialog->setWindowModality(Qt::ApplicationModal); _dialog->open(); 

and here in the switchable slot in ToggleModalDialog

 void ToggleModalDialog::changeModality(bool checkState) { if(checkState) { this->setWindowModality(Qt::NonModal); ui->changeModalityButton->setChecked(true); this->setModal(false); } else { this->setWindowModality(Qt::ApplicationModal); ui->changeModalityButton->setChecked(true); } 

Thanks in advance!

+4
source share
1 answer

You can use either QDialog::setModal(bool) or setWindowModality(Qt::ApplicationModal) . But the documentation for setWindowModality() talks about something extra that is ..

 Changing this property while the window is visible has no effect; you must hide() the widget first, then show() it again. 

So your code should look like this.

 void ToggleModalDialog::changeModality(bool checkState) { if(checkState) { this->setWindowModality(Qt::NonModal); ui->changeModalityButton->setChecked(true); } else { this->setWindowModality(Qt::ApplicationModal); ui->changeModalityButton->setChecked(true); } this->hide(); this->show(); } 
+8
source

Source: https://habr.com/ru/post/1414324/


All Articles