When we create a Qt GUI project, main
the function will generate similar in the following code:
int main(int argc, char *argv[])
{
QApplication application(argc, argv);
CQDialog dialog(NULL);
dialog.show();
return application.exec();
}
Corresponding step is explained as follows
1. Construction of QApplication
the object
2. Construction CQDialog主界面
3. main interface
4. QApplication
objects into the event loop processing until the exit
Comprising the steps above QApplication
objects build process, the main interface display process, three relating to the event loop processing.
This blog mainly on the first topic that QApplication
target the build process.
QApplication
Class hierarchy as shown below
View source Qt QApplication
constructor
#ifdef Q_QDOC
QApplication::QApplication(int &argc, char **argv)
#else
QApplication::QApplication(int &argc, char **argv, int _internal)
#endif
: QGuiApplication(*new QApplicationPrivate(argc, argv, _internal))
{
Q_D(QApplication);
d->init();
}
QApplication
Parent class QGuiApplication
constructor
QGuiApplication::QGuiApplication(QGuiApplicationPrivate &p)
: QCoreApplication(p)
{
}
We can see the QGuiApplication
constructor is empty contents into the QGuiApplication
parent class QCoreApplication
constructor
QCoreApplication::QCoreApplication(QCoreApplicationPrivate &p)
#ifdef QT_NO_QOBJECT
: d_ptr(&p)
#else
: QObject(p, 0)
#endif
{
d_func()->q_ptr = this;
// note: it is the subclasses' job to call
// QCoreApplicationPrivate::eventDispatcher->startingUp();
}
Nor of its actual content.
Mainly in the QApplicationPrivate
, QGuiApplicationPrivate
, QCoreApplicationPrivate
inner classes of treatment, which is consistent Qt usage, that is, information hiding.
Class diagram which follows
Thus the function call returns to the QApplication
constructor QApplicationPrivate::init
function is called for initialization operation
void QApplicationPrivate::init()
{
#if defined(Q_OS_MACOS)
QMacAutoReleasePool pool;
#endif
QGuiApplicationPrivate::init();
initResources();
qt_is_gui_used = (application_type != QApplicationPrivate::Tty);
process_cmdline();
// Must be called before initialize()
qt_init(this, application_type);
initialize();
eventDispatcher->startingUp();
#ifdef QT_EVAL
extern void qt_gui_eval_init(QCoreApplicationPrivate::Type);
qt_gui_eval_init(application_type);
#endif
#ifndef QT_NO_ACCESSIBILITY
// factory for accessible interfaces for widgets shipped with Qt
QAccessible::installFactory(&qAccessibleFactory);
#endif
}
QGuiApplicationPrivate::init
Will be called QCoreApplicationPrivate::init
, QCoreApplicationPrivate::init
will be created eventDispatcher, as shown in the code
#ifndef QT_NO_QOBJECT
// use the event dispatcher created by the app programmer (if any)
if (!eventDispatcher)
eventDispatcher = threadData->eventDispatcher.load();
// otherwise we create one
if (!eventDispatcher)
createEventDispatcher();
Q_ASSERT(eventDispatcher);
if (!eventDispatcher->parent()) {
eventDispatcher->moveToThread(threadData->thread);
eventDispatcher->setParent(q);
}
threadData->eventDispatcher = eventDispatcher;
eventDispatcherReady();
#endif
Based on polymorphisms, QGuiApplicationPrivate::createEventDispatcher
is called
void QGuiApplicationPrivate::createEventDispatcher()
{
Q_ASSERT(!eventDispatcher);
if (platform_integration == 0)
createPlatformIntegration();
// The platform integration should not mess with the event dispatcher
Q_ASSERT(!eventDispatcher);
eventDispatcher = platform_integration->createEventDispatcher();
}
createEventDispatcher
Function in doing two things
1. Create a plug-in platform (Windows, Linux)
2. Create a eventDispatcher according to platform plug-in
In my development on the Windows platform as an example
1. Create QWindowsIntegration
and QWindowsGuiEventDispatcher
2. In the QWindowsIntegration
creation process will generate QWindowsContext
objects
QEventDispatcherWin32
Class hierarchy as shown below
因此,QApplication
构造时创建了eventDispatcher
关于QApplication
对象构建过程就讲述完毕了,后续博文会看到eventDispatcher、QWindowsContext
的用途
有部分代码位于qtbase\src\plugins\platforms源码目录