QT Demo Calqlatr (1) main.cpp

Source: Internet
Author: User

In fact, from the very beginning to learn and analyze QT Demo When selected is the Calqlatr project, but open source a look, seemingly difficult points, this is the above several basic control analysis. Starting with this chapter, I'm going to take calqlatr this demo project.

Main.cpp

The code in Main.cpp is very simple:

#include ". /.. /shared/shared.h "Declarative_example_main (DEMOS/CALQLATR/CALQLATR)

Declarative_example_main (NAME) is a function macro whose implementation is in the ".. /.. /shared/shared.h "in the header file.

In fact, in the previous analysis of the demo also used to use this function macro, but at that time focused on the analysis of QML code, there is no declarative_example_main (NAME) function macro expanded in detail. In this article, it is detailed.

#define DECLARATIVE_EXAMPLE_MAIN (NAME) int MAIN (int argc, char* argv[]) {qguiapplication app (ARGC,ARGV);    App.setorganizationname ("Qt Project");    App.setorganizationdomain ("qt-project.org");    App.setapplicationname (Qfileinfo (App.applicationfilepath ()). BaseName ());    Qquickview view;        if (qgetenv ("Qt_quick_core_profile"). ToInt ()) {Qsurfaceformat F = view.format ();        F.setprofile (Qsurfaceformat::coreprofile);        F.setversion (4, 4);    View.setformat (f);    } view.connect (View.engine (), SIGNAL (Quit ()), &app, SLOT (Quit ()));    New Qqmlfileselector (View.engine (), &view);     View.setsource (Qurl ("qrc:///" #NAME ". qml"));    View.setresizemode (Qquickview::sizerootobjecttoview);           if (qguiapplication::p latformname () = = Qlatin1string ("QNX") | |    Qguiapplication::p latformname () = = Qlatin1string ("Eglfs")) {View.showfullscreen ();    } else {view.show (); } return App.exec ();}

From the implementation of the macro can be seen to clean, is actually a common main () function implementation.

This main () and the basic structure of the main () function, which we used to analyze the window demo program, are identical, but there are differences: in the window demo, we use Qqmlcomponent, which is used by Qquickview.

Qguiapplication app Because this is to create a GUI program, so use Qguiapplication. However, the following three lines are used in the main () function of the previous window demo program:

    App.setorganizationname ("Qt Project");    App.setorganizationdomain ("qt-project.org");    App.setapplicationname (Qfileinfo (App.applicationfilepath ()). BaseName ());

Let's take a look at the official descriptions of the two properties of OrganizationName and Organizationdomain:

Organizationname:qstring
The holds the name of the organization that wrote this application.
The value is used by the Qsettings class while it is constructed using the empty constructor.

Organizationdomain:qstring
The holds the Internet domain of the organization that wrote this application.
The value is used by the Qsettings class while it is constructed using the empty constructor.

See here, We know that both the OrganizationName and Organizationdomain properties are actually used in qsettings or other OrganizationName and organizationdomain that need to display the app.


In the same way, the ApplicationName attribute is also used when qsettings or other name needs to display the app, so it's clear that the app's title default value is the value of applicationname.

Qquickview View

The Qquickview class provides a window for displaying a Qt Quick user interface.
This is a convenience subclass of Qquickwindow which would automatically load and display a QML scene when given the URL of The main source file. Alternatively, you can instantiate your own objects using qqmlcomponent and place them in a manually setup Qquickwindow.

As you can see from the official note above, the Qquickview class is a subclass of the Qquickwindow class and is a convenient way to create windows, but you need to use Qqmlcomponent to load QML methods with respect to Qquickwindow. Qquickview is the direct extension of the SetSource method, here is an example:

Qquickview *view = new Qquickview;view->setsource (Qurl::fromlocalfile ("myqmlfile.qml")); View->show ();

The qml file that needs to be loaded is also specified here by SetSource:

    View.setsource (Qurl ("qrc:///" #NAME ". qml")); \
The above #name is the parameter of the Declarative_example_main (NAME) function macro.

View.Setresizemode()

The resize mode is set in the code:

    View.setresizemode (qquickview::sizerootobjecttoview); \

From official documents, there are two types of resize modes, namely:

    • Qquickview::sizeviewtorootobject:the view resizes with the root item in the QML.
    • Qquickview::sizerootobjecttoview:the view would automatically resize the root item to the size of the view.

But from the name point of view, these two patterns from view to Rootobject, one from the rootobject to the view, when exactly when to use Which? The instructions here are simple, take a look at the detailed description of the detailed description section:

Qquickview also manages sizing of the view and root object.  By default, the ResizeMode is Sizeviewtorootobject, which would load the component and resize it to the size of the view. Alternatively the ResizeMode may is set to Sizerootobjecttoview which would resize the view to the size of the root object .

Then we can simply understand how to automatically adjust the size of the root object and the view, but here is the so-called root object exactly what, no detailed explanation. Here is a todo:sizeviewtorootobject and sizerootobjecttoview difference and how to use them.

View.Show()

After you have set the properties of the view, you can display the view, and in the example, use the following code to display the view:

    if (qguiapplication::p latformname () = = Qlatin1string ("QNX") | |           Qguiapplication::p latformname () = = Qlatin1string ("Eglfs")) {        view.showfullscreen ();    } else {        view.show ( );    } \

Note that Qguiapplication is mentioned here::p Latformname (), the above-judged QNX and Eglfs are unfamiliar to us, but by looking at the documentation we learned Qguiapplication::p latformname () Also includes several:

    • Android
    • Cocoa is a platform plugin for MAC OS x.
    • Windows
    • Ios
    • XCB is the X11 plugin used on regular desktop Linux platforms.
    • ...

Then we can easily determine whether the current platform is OS X, Linux, Windows and Android, iOS and so on, as to which some of the less common platforms, for me, limited ability to expand.

Once the platform information has been obtained, you can choose how to display the view, with full-screen display for QNX and Eglfs in the example, and others in the default display of the platform. In addition to the above two display methods, there are some of the following functions to specify how to display:

    • Show (): Shows the window, depending on the platform's default behavior for the window type and flags.
    • Showfullscreen (): Shows the window as fullscreen.
    • Showmaximized (): Shows the window as maximized.
    • Showminimized (): Shows the window as minimized.
    • Shownormal (): Shows the window as normal, i.e neither maximized, minimized, nor fullscreen.
Qsurfaceformat

After the view is created, there is the following code:

    if (qgetenv ("Qt_quick_core_profile"). ToInt ()) {        Qsurfaceformat f = View.format ();        F.setprofile (qsurfaceformat::coreprofile);        F.setversion (4, 4);        View.setformat (f);    } \

By adding a print, we find that if this is not done at all, and the global search qt_quick_core_profile has not found any other place to define the env, so the part of the code is not analyzed.

Summarize

This chapter is just a detailed analysis of the declarative_example_main(NAME) function macro that will be seen in almost every demo project. Starting with the next chapter, go to Calqlatr's QML code.

QT Demo Calqlatr (1) main.cpp

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.