PDA

View Full Version : Qt DECLARATIVE_EXAMPLE_MAIN means what?



Mike_m
27th June 2017, 09:37
I wanted to look at an example C++ code that came with Qt software.
I looked at main.cpp file at following path:


C:\Qt\Examples\Qt-5.9\quick\threading


This main.cpp has following two lines:


#include "../shared/shared.h"
DECLARATIVE_EXAMPLE_MAIN(threading/threading)


What this DECLARATIVE_EXAMPLE_MAIN means?
Is there a way to look at this main.cpp's C++ source code and understand features of this main program
please?
Thanks

Mike_m
28th June 2017, 12:09
I found answer to what DECLARATIVE_EXAMPLE_MAIN means?
This is a macro defined in the following shared.h file.



// C:\Qt\Examples\Qt-5.9\quick\shared\shared.h

#define DECLARATIVE_EXAMPLE_MAIN(NAME) int main(int argc, char* argv[]) \
{\
QCoreApplication::setAttribute(Qt::AA_EnableHighDp iScaling);\
QGuiApplication app(argc,argv);\
app.setOrganizationName("QtProject");\
app.setOrganizationDomain("qt-project.org");\
app.setApplicationName(QFileInfo(app.applicationFi lePath()).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(), &QQmlEngine::quit, &app, &QCoreApplication::quit);\
new QQmlFileSelector(view.engine(), &view);\
view.setSource(QUrl("qrc:///" #NAME ".qml")); \
if (view.status() == QQuickView::Error)\
return -1;\
view.setResizeMode(QQuickView::SizeRootObjectToVie w);\
if (QGuiApplication::platformName() == QLatin1String("qnx") || \
QGuiApplication::platformName() == QLatin1String("eglfs")) {\
view.showFullScreen();\
} else {\
view.show();\
}\
return app.exec();\
}

d_stranz
28th June 2017, 17:07
And do you understand what it does?

Mike_m
30th June 2017, 07:02
To understand what it does, I replaced macro with its equivalent C++ program as follows:


#include <QDir>
#include <QGuiApplication>
#include <QQmlEngine>
#include <QQmlFileSelector>
#include <QQuickView>

int main(int argc, char* argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDp iScaling);
QGuiApplication app(argc,argv);
app.setOrganizationName("QtProject");
app.setOrganizationDomain("qt-project.org");
app.setApplicationName(QFileInfo(app.applicationFi lePath()).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(), &QQmlEngine::quit, &app, &QCoreApplication::quit);
new QQmlFileSelector(view.engine(), &view);
view.setSource(QUrl("qrc:///threading/threading.qml"));
if (view.status() == QQuickView::Error)
return -1;
view.setResizeMode(QQuickView::SizeRootObjectToVie w);
if (QGuiApplication::platformName() == QLatin1String("qnx") ||
QGuiApplication::platformName() == QLatin1String("eglfs")) {
view.showFullScreen();
} else {
view.show();
}
return app.exec();
}



This program gave following output:

12507

In the next post, I will explain this program and its output.

d_stranz
30th June 2017, 17:43
Good. The purpose of the macro is to give the developers of QML examples the ability to write a "boilerplate" driver program, where all of the differences between examples are in the QML that the driver loads in the "view.setSource()" line 23. The driver is identical in all examples, what is different is only in the QML.

Mike_m
1st July 2017, 03:19
d_stranz,
Thank you for information about line 23.
I quickly searched all example programs and found following is list of example programs this boilerplate driver is being used:



DECLARATIVE_EXAMPLE_MAIN(qml/xmlhttprequest/xmlhttprequest)
DECLARATIVE_EXAMPLE_MAIN(animation/animation)
DECLARATIVE_EXAMPLE_MAIN(canvas/canvas)
DECLARATIVE_EXAMPLE_MAIN(dialcontrol)
DECLARATIVE_EXAMPLE_MAIN(demos/calqlatr/calqlatr)
DECLARATIVE_EXAMPLE_MAIN(demos/clocks/clocks)
DECLARATIVE_EXAMPLE_MAIN(demos/maroon/maroon)
DECLARATIVE_EXAMPLE_MAIN(demos/rssnews/rssnews)
DECLARATIVE_EXAMPLE_MAIN(demos/samegame/samegame)
DECLARATIVE_EXAMPLE_MAIN(demos/stocqt/stocqt)
DECLARATIVE_EXAMPLE_MAIN(demos/tweetsearch/tweetsearch)
DECLARATIVE_EXAMPLE_MAIN(draganddrop/draganddrop)
DECLARATIVE_EXAMPLE_MAIN(externaldraganddrop/externaldraganddrop)
DECLARATIVE_EXAMPLE_MAIN(imageelements/imageelements)
DECLARATIVE_EXAMPLE_MAIN(keyinteraction/keyinteraction)
DECLARATIVE_EXAMPLE_MAIN(mousearea/mousearea)
DECLARATIVE_EXAMPLE_MAIN(particles/affectors/affectors)
DECLARATIVE_EXAMPLE_MAIN(particles/customparticle/customparticle)
DECLARATIVE_EXAMPLE_MAIN(particles/emitters/emitters)
DECLARATIVE_EXAMPLE_MAIN(particles/imageparticle/imageparticle)
DECLARATIVE_EXAMPLE_MAIN(particles/system/system)
DECLARATIVE_EXAMPLE_MAIN(positioners/positioners)
DECLARATIVE_EXAMPLE_MAIN(accessibility/accessibility)
DECLARATIVE_EXAMPLE_MAIN(righttoleft/righttoleft)
DECLARATIVE_EXAMPLE_MAIN(shadereffects/shadereffects)
DECLARATIVE_EXAMPLE_MAIN(text/text)
DECLARATIVE_EXAMPLE_MAIN(threading/threading)
DECLARATIVE_EXAMPLE_MAIN(touchinteraction/touchinteraction)
DECLARATIVE_EXAMPLE_MAIN(views/views)