Qt QApplication object event distribution mechanism for source code analysis of the construction process

When we create a Qt GUI project, mainthe 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 QApplicationthe object
2. Construction CQDialog主界面
3. main interface
4. QApplicationobjects into the event loop processing until the exit

Comprising the steps above QApplicationobjects build process, the main interface display process, three relating to the event loop processing.

This blog mainly on the first topic that QApplicationtarget the build process.

QApplicationClass hierarchy as shown below

View source Qt QApplicationconstructor

#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();
}

QApplicationParent class QGuiApplicationconstructor

QGuiApplication::QGuiApplication(QGuiApplicationPrivate &p)
    : QCoreApplication(p)
{
}

We can see the QGuiApplicationconstructor is empty contents into the QGuiApplicationparent class QCoreApplicationconstructor

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, QCoreApplicationPrivateinner classes of treatment, which is consistent Qt usage, that is, information hiding.

Class diagram which follows

Thus the function call returns to the QApplicationconstructor QApplicationPrivate::initfunction 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::initWill be called QCoreApplicationPrivate::init, QCoreApplicationPrivate::initwill 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::createEventDispatcheris 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();
}

createEventDispatcherFunction 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 QWindowsIntegrationand QWindowsGuiEventDispatcher
2. In the QWindowsIntegrationcreation process will generate QWindowsContextobjects

QEventDispatcherWin32Class hierarchy as shown below

因此,QApplication构造时创建了eventDispatcher

关于QApplication对象构建过程就讲述完毕了,后续博文会看到eventDispatcher、QWindowsContext的用途

有部分代码位于qtbase\src\plugins\platforms源码目录

Guess you like

Origin www.cnblogs.com/appsucc/p/12004967.html