开发者

Qt Asynchronous Action During aboutToQuit

开发者 https://www.devze.com 2022-12-21 04:23 出处:网络
I\'ve got some Asynchronous cleanup activity I need to do as my Qt application is shutting down. My current approach is to trap the aboutToQuit signal. I launch my Asynchronous behavior, and then the

I've got some Asynchronous cleanup activity I need to do as my Qt application is shutting down.

My current approach is to trap the aboutToQuit signal. I launch my Asynchronous behavior, and then the application shuts down. Is there any way to block Qt from shutting down unt开发者_C百科il my asynchronous behavior is complete?

This works on windows by trapping the main window's closeEvent and calling ignore() on the event. But on Mac, when a user quits, my ignore() isn't honored, and the Application shuts down before my asynchronous activity is complete.

Thanks.


Why do you launch your cleanup code asynchronously if you have to wait anyway until your code is done?

If you don't connect your slot as QueuedConnection to aboutToQuit it should block until your cleanup code is done.

But if you really want launch it asynchronously you have to synchronize it by hand:

QSemaphore wait4CleanupDone;

class MyQuitHandler { ... public slots: void handleQuit(); ... } myQuitHandler;
void MyQuitHandler::handleQuit() { ... ; wait4CleanupDone.release(); }

int main(int argc, char** argv) {
  QApplication app(argc, argv);
  QObject::connect(&app, SIGNAL(aboutToQuit()), &myQuitHandler, SLOT(handleQuit()));
  ...
  int result = app.exec();
  wait4CleanupDone.acquire();
  return result;
}

But note, that your asynchronous code might be ignored in some cases anyway:

"We recommend that you connect clean-up code to the aboutToQuit() signal, instead of putting it in your application's main() function. This is because, on some platforms the QApplication::exec() call may not return. For example, on the Windows platform, when the user logs off, the system terminates the process after Qt closes all top-level windows. Hence, there is no guarantee that the application will have time to exit its event loop and execute code at the end of the main() function, after the QApplication::exec() call."

From: http://doc.trolltech.com/4.6/qapplication.html#exec


Crashes here regrettably when i try this.

problem is the connect call where it alraedy crashes line 3 of main.

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    QObject::connect((QObject *)&app, SIGNAL(aboutToQuit()), (QObject *)&myQuitHandler, 
    SLOT(QuitApp()), Qt::QueuedConnection );

    QGraphicsScene scene(QRectf(0, 0, 640, 480));
    ..
}

After i started using qgraphicsscene not a single signal works anymore as i get it not connected in a manner that any signal works.

So far could solve everything, how to solve this without 'connect' ?

Crash i get here is instant:

Invalid parameter passed to C runtime function. exited with code -1073741819

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号