Close non-modal window when a model window is opened?
-
Hi, the below code executes but does not close my widget window:
void MyWidget::onAppFocusWindowChanged(QWindow* focusWindow) { if(this->isVisible() && focusWindow->isModal()) this->close(); }
MyWidget
is non-modal window, the above slot is connected to thevoid QGuiApplication::focusObjectChanged(QObject *focusObject)
signal. The slot executes when I open a modal window andfocusWindow->isModal()
returns true, but the instance ofMyWidget
is not closed, why? -
@CJha said in Close non-modal window when a model window is opened?:
why?
Does your app enter the if block (so, is this->close() executed)?
-
@jsulm Yes, it enters the block and executes any code I put there, I tried:
void MyWidget::onAppFocusWindowChanged(QWindow* focusWindow) { if(this->isVisible() && focusWindow->isModal()){ qDebug() << "Will Close"; this->close(); } }
void MyWidget::onAppFocusWindowChanged(QWindow* focusWindow) { if(this->isVisible() && focusWindow->isModal()) this->hide(); }
Both
qDebug() << "Will Close";
andthis->hide();
works as expected. -
@CJha
This is perhaps a possibility, I'm not sure how modal blocking loop when other messages arrive.show()
&hide()
do not work immediately as they are executed, rather they mark the window for closure. You do not see it close till the next time the main event loop sees it and visibly closes the window. So, is it possible/the case that the visual closure of the other window does not appear while the modal/blocking window is still present? And the closure happens after you exit the modal dialog's blocking loop? -
@JonB No, it does not close the non-modal window. I installed the event filter on non-modal window to see if the
QEvent::Close
event gets delivered or not and it never gets delivered to the non-modal window, not even after the modal window is closed. It appears thatthis->close()
is completely ignored. -
Windows can't be closed when a modal window is shown as per this comment.
As to why - the idea of a modal window is that it "blocks" the execution of the rest of the ui while it is visible. That includes closing windows. If you want to close the window when the modal dialog is shown call close before you call dialog's show or execute methods.
-
@Chris-Kawa said in Close non-modal window when a model window is opened?:
Windows can't be closed when a modal window is shown as per this comment.
Perfect, the sort of thing I wondered if it might be the case.
If you want to close the window when the modal dialog is shown call close before you call dialog's show or execute methods.
Sounds excellent to me!
-
@Chris-Kawa Ok, I understand and it does make sense.
In my case, my non-modal window has
Qt::WindowStaysOnTopHint
set, if the user opens a modal window that by chance happens to be placed under my non-modal window then the entire application gets blocked. Because of this reason I was trying to close my non-modal window if the new window that gets the focus is a modal window. -
@CJha I would say you should rather remove the always on top flag before you show the modal window and restore it afterwards if needed.
-
@Chris-Kawa Those are 2 completely different parts of the application, one does not have any idea of what the other part is showing that's why I was using the
void QGuiApplication::focusWindowChanged(QWindow *focusWindow)
signal to observe the change. I noticed that thethis->hide()
works for the non-modal window and it gets the job done for me.