Qt程序是基于事件驱动的,在每个GUI主线程的main函数后面都有return a.exec();
实际上就是使Qt程序进入事件循环。
下面我们看看Qt exec()的源码:
int QCoreApplication::exec()
{
if (!QCoreApplicationPrivate::checkInstance("exec"))
return -1;
//获取线程数据
QThreadData *threadData = self->d_func()->threadData;
//判断是否在主线程创建
if (threadData != QThreadData::current()) {
qWarning("%s::exec: Must be called from the main thread", self->metaObject()->className());
return -1;
}
//判断eventLoop是否已经创建
if (!threadData->eventLoops.isEmpty()) {
qWarning("QCoreApplication::exec: The event loop is already running");
return -1;
}
threadData->quitNow = false;
QEventLoop eventLoop;
self->d_func()->in_exec = true;
//创建eventLoop
int returnCode = eventLoop.exec();
threadData->quitNow = false;
if (self) {
self-&g