Code lines not executed when overwriting the close event
-
Hello there,
I am trying to overwrite the close event function as I want to display some messages on mainwindow indicating what it is doing before closing the window
The overwrite of the function looks like this
void MainWindow::closeEvent(QCloseEvent *event) { QMessageBox::StandardButton resBtn = QMessageBox::question( this, "Close INTEGER", tr("Are you sure?\n"), QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes, QMessageBox::Yes); if (resBtn != QMessageBox::Yes) { event->ignore(); } else { // do something delete wlogger; event->accept(); } }
However, the "do something" lines have no effect on the mainwindow (what I am trying to do is to append some text in the QPlaintext widget on the mainwindow telling the user what the app is doing while closing).
Am I missing something?Thanks for your help
-
@Gaetano03 said in Code lines not executed when overwriting the close event:
However, the "do something" lines have no effect on the mainwindow (what I am trying to do is to append some text in the QPlaintext widget on the mainwindow telling the user what the app is doing while closing).
This is not what the code you posted is doing.
And I do not understand the use case: how long does it take for your app to close that it would make sense to tell user what the app is doing while closing? -
it can take up to 30 secs, linked to the closing of the logger stream (a different thread which is writing log info on file and appending also to a QPlainText widget).
So, when I close the app, it can takes some time to execute the delete wlogger which closes the stream
-
@Gaetano03 said in Code lines not executed when overwriting the close event:
and appending also to a QPlainText widget
I hope you do not mean the other thread is actually directly appending/writing to a widget, rather it is sending signals for your UI thread to do so on its behalf!?
However, the "do something" lines have no effect on the mainwindow
Your "do something" lines are being executed inside a slot. You will not see widget updates until the main Qt event loop runs again, after the slot has finished.
-
@Gaetano03 said in Code lines not executed when overwriting the close event:
I am sending signals using a function pointer
Good :) Sorry I answer many questions so I don't recall old ones :)
But that does not obviate my second point:
Your "do something" lines are being executed inside a slot. You will not see widget updates until the main Qt event loop runs again, after the slot has finished.
which I presume is the reason for your
However, the "do something" lines have no effect on the mainwindow
?
-
@Gaetano03
A good question!I can see two possible approaches:
-
When you are in your "
/// do something
" callQCoreApplication::processEvents()
after updating your widget. That will allow the new content to be shown. I don't normally like this, and it worries me called from inside a slot, but it may be "quick & dirty" way to get what you want. Or maybe void QWidget::repaint() on your QPlaintext widget will show the updates immediately. -
Use a QProgressDialog to show your text widget. Run the progress dialog in modal mode, as shown in the first example at https://doc.qt.io/qt-5/qprogressdialog.html#details. Note that this effectively calls
QCoreApplication::processEvents()
like above, as per https://doc.qt.io/qt-5/qprogressdialog.html#value-prop.
There are other ways, don't want to type them all up. Do either of the above suffice?
Unless a Qt expert can think of a better alternative?
-
-
@Gaetano03 said in Code lines not executed when overwriting the close event:
Thanks, I am using repaint for now and it works
Yeah, dirty but simple :)
Try to be good about it by setting a flag when you are in the "exiting" case and only have your plaintext widget code use the
repaint()
when set, so you don't do it the rest of the time.