Qapplication Initialization of QT analysis

Source: Internet
Author: User

Original address: http://blog.163.com/net_worm/blog/static/1277024192010097430321/

Before starting the analysis, one is the analysis of QT in the Window platform implementation (other OS similar); second, the analysis of the means to see the source + single-step tracking. Sometimes the compiler produces pre-compiled output (using the-e parameter), which makes it easy to observe; third, the QT version of 4.5.3

The following is the QT classic Hello World program

1#include <qapplication.h>2#include <qpushbutton.h>3#include <qfont.h>4 5 intMainintargcChar**argv)6 {7 qapplication A (argc, argv);8Qpushbutton Quit ("Quit",0 );9Quit.resize ( the, - );TenQuit.setfont (Qfont (" Times", -, Qfont::bold)); OneQobject::connect (&quit, SIGNAL (clicked ()), &A, SLOT (Quit ())); A quit.show (); -  returna.exec (); -}

The first sentence declares a Qapplication object, and the Qapplication class inheritance relationship is:

1 QApplication:QCoreApplication:QObject

First observe the Qapplication constructor:

1 qapplication::qapplication (intChar * *argv)2     : qcoreapplication (*New  qapplicationprivate (argc, argv, guiclient)) 3 {q_d (qapplication); D->construct ();}

Here have done three things: New into a Qapplicationprivate object and passed to qcoreapplication; macro q_d; Call D->construct ().

Look at the definition of q_d:

1 #define Q_d (Class) class# #Private * Const D = D_func ()

Q_d (qapplication); when expanded:

1 const d = D_func ();

In the vicinity just see the definition of Q_Q:

1 #define Q_Q (Class) class * Const Q = Q_func ()

As a result, it is best not to define variables such as D and Q in the QT program.

So the d->construct () call is actually

1 qapplicationprivate::construct ();

So what did this construct () do?

1 voidqapplicationprivate::construct ()2 {3Initresources ();//Initializing Resources4 5qt_is_gui_used = (Qt_apptype! =Qapplication::tty); 6Process_cmdline ();//Scan Analysis command-line arguments7     //must be called before Initialize ()8Qt_init ( This, Qt_apptype);//initialization that must be performed before initialize (), such as color, font, keyboard, etc.9 Initialize ();TenEventdispatcher->Startingup (); One  A}

where Eventdispatcher->startingup (); The actual invocation is: Qeventdispatcherwin32::startingup (), mainly registering the event dispatcher.

Come back to see Qcoreapplication's construction process, pass the Qapplicationprivate object to Qojbec, and then call Init ().

1 qcoreapplication::qcoreapplication (qcoreapplicationprivate &P)2     0 )3{4    init (); 5     // Note:it is the subclasses ' job to call 6     // Qcoreapplicationprivate::eventdispatcher->startingup (); 7 }

Let's see what Qcoreapplication::init () has done first.

1 voidQcoreapplication::init ()2 {3 q_d (qcoreapplication);4 5     //Get The application name/instance if Qwinmain () is not invoked6Set_winapp_name ();//set the name of the application7 8Q_assert_x (!self,"qcoreapplication","There should is only one Application object");9Qcoreapplication::self = This;Ten  OneQthread::initialize ();//Initializing Threads A  -     //Use the event dispatcher created by the app programmer (if any) -     if(!qcoreapplicationprivate::eventdispatcher) theQcoreapplicationprivate::eventdispatcher = d->threaddata->Eventdispatcher; -     //Otherwise we create one -     if(!qcoreapplicationprivate::eventdispatcher) -D->createeventdispatcher ();//Generate event Distribution +Q_assert (Qcoreapplicationprivate::eventdispatcher! =0); -  +     if(! Qcoreapplicationprivate::eventdispatcher->parent ()) AQcoreapplicationprivate::eventdispatcher->movetothread (d->threaddata->thread); at  -D->threaddata->eventdispatcher =Qcoreapplicationprivate::eventdispatcher; -  -     if(!coreappdata ()app_libpaths) { -         //Make sure this library paths is initialized -Librarypaths ();//confirm or set the path to the DLL library in}Else { -D->appendapplicationpathtolibrarypaths (); to     } +  -Qt_startup_hook ();//currently an empty function the}

What does the construction of object do?

Qobject::qobject (qobjectprivate &dd, Qobject *parent): D_ptr (&DD) passes Qapplicationprivate object to D_ptr, this d_ What is PTR?

We look at the Qobject class definition, D_ptr is the only data member in Qobject:

Protected:qobjectdata *d_ptr; At this point, the initial basic analysis of the Qapplication object, in addition to many of the initialization of the action, the main thing is to associate Qapplication and Qapplicationprivate.

Initialization of qapplication for QT analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.