QT Source View 001-qapplication and Qcoreapplication

Source: Internet
Author: User

  The difference between qcoreapplication and qapplication (1)

Qapplication This class is inherited qcoreapplication, and Qcoreapplication has inherited Qobject, and Qobject is the most basic class of QT, that is, the foundation of QT.

Let's start with Qcoreapplicatio, this is the beginning of the header file.

class  Public Qobjec
{
Static Qcoreapplication *instance () {return self;}

}

1. What is Q_core_export? If a DLL symbol is defined when writing a dynamic library, Q_gui_export is an export function or class, and if it is used in the application, the DLL symbol is not defined, and Q_gui_export is the import class or function, which of course is imported, and we write the command line, Instead of writing a dynamic library, here are some definitions of functions and variables

2. Because it is static qcoreapplication *instance () {return self;} is to return a self

static Qcoreapplication *self; is a private statically member variable that is implemented outside the class qcoreapplication *qcoreapplication::self = 0;

it is necessary to take a look at the constructor of this qcoreapplication class Qcoreapplication::qcoreapplication (int&ARGC,Char**argv): Qobject (*Newqcoreapplicationprivate (argc, argv))    {init (); Qcoreapplicationprivate::eventdispatcher-Startingup ();#ifDefined (Q_os_symbian) &&!defined (qt_no_library)//Refresh Factoryloader, as text codecs is requested during Lib path//resolving process and won ' t be therefore properly loaded. //Unknown If this is Symbian specific issue.Qfactoryloader::refreshall ();#endif#ifDefined (Q_os_symbian) &&!defined (Qt_no_systemlocale)D_func ()-symbianinit ();#endif}

The following looks a bit dizzy, here just see Init (); This function, just below the constructor, has more code here,

This is just a line of our attention. void Qcoreapplication::init () {     this;}

The self is assigned here, which makes the self point to the current object, not nothing, and the self can be used instead of the current object.

Of course, this can only be within the class, because self is private, to use outside of the class should define a common member function what, first leave the question here?

It turns out we see this code on the previous line of the class definition

#define Qapp qcoreapplication::instance () defines a Qapp macro, which is a pointer to its own

What's the use of it? When we define the Qcoreapplication app (argc, argv) in the main program, the object is completely unnecessary to use QAPP this macro, but what if the main function to use this object to do, pass? This is more troublesome, QT with this point to their own east is to help us to solve the main function outside the use of the object of the application can not find the object of distress. Well, you can use qapp outside of the function, so what's the problem? Is this thing in memory? Hey, this is static Ah, just in memory, bold to use this point to their own macro pointer bar, as long as the app did not deconstruct this stuff has been in memory.

Here is a description of qapplication, this is inherited from Qcoreapplication, #define Qapp (static_cast<qapplication *> (Qcoreapplication::instance ()))

There is a code like this before the class definition, and here is a pointer to itself, but to do a type conversion, as to whether this type conversion is safe, I think it should be safe, because the equivalent is from the base class to the derived class, some of the base class should be copy, But the static here will not be very clear about this problem, in short, to convert this qapp can be used outside the main function.

Here also to explain is this qcoreapplication is not a single case problem, there are many people on the Internet think is a single case, there are many people agree, but I practice a bit should not be a single case

for (int i = 0; i < 3; ++i)
{
Qcoreapplication app;
}

This kind of operation is allowed, because the previous has been deconstructed, Qt said only allowed to create a function is only to create one, here is obviously not, the implementation of the singleton is generally through the private constructor to achieve, here the constructors are common is obviously not a singleton rhythm.

the difference between Qcoreapplication and qapplication (2)

1. Qcoreapplication Applications for Non-gui (no need to rely on Qtgui libraries), qapplication for Applications that contain GUIs (Qtgui libraries are required).

2. Qapplication inherited the Qguiapplication class, and Qguiapplication inherited the Qcoreapplication class, and Qcoreapplication inherited Qobject, And Qobject is the most basic class of QT, which is the basis of QT.

Endl

QT Source View 001-qapplication and Qcoreapplication

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.