Close button dialog without closeEvent
-
Hi,
I'm trying to setup a prompt message when the "close" button is pressed. Currently, I'm doing something like this:
void ShoulderControl::closeEvent(QCloseEvent *event) { QMessageBox::StandardButton reply; reply = QMessageBox::question(this, "App", "You are about to close this window, have you saved the settings?", QMessageBox::Yes|QMessageBox::No); if (reply == QMessageBox::Yes) { event->accept(); } else if(reply == QMessageBox::No) { event->ignore(); } }
My requirement is to show the message only when a user presses the close button, NOT when by window closes on its own. Any suggestions on possible workarounds?
-
Hi,
I'm trying to setup a prompt message when the "close" button is pressed. Currently, I'm doing something like this:
void ShoulderControl::closeEvent(QCloseEvent *event) { QMessageBox::StandardButton reply; reply = QMessageBox::question(this, "App", "You are about to close this window, have you saved the settings?", QMessageBox::Yes|QMessageBox::No); if (reply == QMessageBox::Yes) { event->accept(); } else if(reply == QMessageBox::No) { event->ignore(); } }
My requirement is to show the message only when a user presses the close button, NOT when by window closes on its own. Any suggestions on possible workarounds?
@viniltc said in Close button dialog without closeEvent:
NOT when by window closes on its own.
What precisely does this mean? Windows do not "close on their own", just because they feel like it :) Do you mean your code may call
ShoulderControl::close()
explicitly? In that case use theQCloseEvent *event
parameter to check the value of event->spontaneous() to distinguish. -
@viniltc said in Close button dialog without closeEvent:
NOT when by window closes on its own.
What precisely does this mean? Windows do not "close on their own", just because they feel like it :) Do you mean your code may call
ShoulderControl::close()
explicitly? In that case use theQCloseEvent *event
parameter to check the value of event->spontaneous() to distinguish. -
@JonB there are multiple windows in my application, and I sometimes switch between windows by closing one or another. I do not want to show the
closeEvent
message every time unless the user presses the close button on the window.@viniltc said in Close button dialog without closeEvent:
and I sometimes switch between windows by closing one or another
I really don't know what you mean. "Switching between windows" does not generate close events, and closing one window does not generate a close event in a different window. You can close a window by clicking its Close button ---
QEvent::spontaneous() == true
--- or by callingclose()
from code ---QEvent::spontaneous() == false
. Which is what you asked to distinguish. -
@JonB thanks a lot , this worked for me!
void ShoulderControl::closeEvent(QCloseEvent *event) { if(event->spontaneous()){ QMessageBox::StandardButton reply; reply = QMessageBox::question(this, "App", "Are you sure want to close this window?", QMessageBox::Yes|QMessageBox::No); if (reply == QMessageBox::Yes) { event->accept(); } else if(reply == QMessageBox::No) { event->ignore(); } } }