This wasn't immediately clear to me from the docs for QCoreApplication::quit().
Are any pending events in the event loop cancelled when the quit() slot is i开发者_运维问答nvoked?
Calling QCoreApplication::quit()
is the same as calling QCoreApplication::exit(0)
. There it says
After this function has been called, the application leaves the main event loop and returns from the call to exec().
Since the event loop is left, I would think any pending events are cancelled.
Edit: I made a small test case to show that pending events are indeed cancelled:
#include <QCoreApplication>
#include <QTimer>
#include <QDebug>
class MyObject : public QObject
{
Q_OBJECT
public Q_SLOTS:
void start()
{
QCoreApplication::postEvent(this, new QEvent(QEvent::User));
QCoreApplication::quit();
}
protected:
void customEvent(QEvent* event)
{
qDebug() << "Event!";
}
};
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
MyObject o;
QTimer::singleShot(0, &o, SLOT(start()));
return app.exec();
}
#include "main.moc"
In this case, the event posted in MyObject::start()
will never arrive. It will, of course, if you remove the call to QCoreApplication::quit()
.
精彩评论