End application call twice closeevent

I wrote an application in Qt / C ++ on OSX. When you exit the application, I will catch a closeevent to display a dialog box

void MainUI::closeEvent (QCloseEvent *event) { if( DeviceUnplugged == false) { ExitDialog = new DialogExit; ExitDialog->exec(); if(ExitDialog->result() == QDialog::Accepted) { m_device.CloseDevice(); event->accept(); } else { event->ignore(); } } } 

The dialog box displays correctly when closed using the red cross or the quit menu.

but when I close the application by right-clicking on the icon in the dock, the dialog box appears twice when the close event is fired twice.

Any idea why?

+6
source share
2 answers

Yes, I think this is normal for a Mac, at least I have it in a Qt app too (Mac only).

I used the following workaround:

 void MainUI::closeEvent (QCloseEvent *event) { if (m_closing) { event->accept(); return; } if( DeviceUnplugged == false) { ExitDialog = new DialogExit; ExitDialog->exec(); if(ExitDialog->result() == QDialog::Accepted) { m_device.CloseDevice(); m_closing = true; event->accept(); } else { event->ignore(); } } } 

By default, the boolean variable m_ closing should be initialized to false , of course, in your class. Thus, the second time nothing will be done (processing will be skipped). It worked for me.

+2
source

This seems to be a QT error: See: https://bugreports.qt.io/browse/QTBUG-43344

This issue also occurred when using qt-5.6_4,
In my case, this happened when using CMD + Q, but did not happen when using the red x button.

A similar patch is used.
I avoided accepting or ignoring , as this is a mistake, and I don’t think we should “talk to her” :-)

Instead, I just return when called more than once.

 static int numCalled = 0; if (numCalled++ >= 1) return; 
+1
source

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


All Articles