From 1070aeb5521a95039cad3f4536e85e99526a4900 Mon Sep 17 00:00:00 2001 From: J-P Nurmi <jpnurmi@digia.com> Date: Wed, 27 Nov 2013 17:38:12 +0100 Subject: [PATCH] Revise main() in examples Replace the inconvenient QT_QUICK_CONTROLS_EXAMPLE_MAIN macro with a simpler macro that merely defines the actual application type. This way it's easier to do tweaks to main() when for example testing a different application font size. Moreover, browsing the example code becomes more convenient (especially online). Task-number: QTBUG-35083 Change-Id: I8203816a9112bf29b4661baa86bf4a196554db05 Reviewed-by: Jens Bache-Wiig <jens.bache-wiig@digia.com> --- .../controls/basiclayouts/basiclayouts.pro | 1 + examples/quick/controls/basiclayouts/main.qml | 1 + .../quick/controls/basiclayouts/src/main.cpp | 11 +++++-- examples/quick/controls/gallery/gallery.pro | 1 + examples/quick/controls/gallery/main.qml | 1 + examples/quick/controls/gallery/src/main.cpp | 11 +++++-- ...plemain.h => qtquickcontrolsapplication.h} | 29 ++++--------------- examples/quick/controls/shared/shared.pri | 4 +++ examples/quick/controls/splitview/main.qml | 1 + .../quick/controls/splitview/splitview.pro | 1 + .../quick/controls/splitview/src/main.cpp | 11 +++++-- examples/quick/controls/tableview/main.qml | 2 +- .../quick/controls/tableview/src/main.cpp | 11 +++++-- .../quick/controls/tableview/tableview.pro | 1 + .../quick/controls/texteditor/qml/main.qml | 1 + .../quick/controls/texteditor/src/main.cpp | 24 ++------------- .../quick/controls/texteditor/texteditor.pro | 1 + examples/quick/controls/touch/main.qml | 1 + examples/quick/controls/touch/src/main.cpp | 11 +++++-- examples/quick/controls/touch/touch.pro | 1 + 20 files changed, 69 insertions(+), 56 deletions(-) rename examples/quick/controls/shared/{qt_quick_controls_examplemain.h => qtquickcontrolsapplication.h} (73%) create mode 100644 examples/quick/controls/shared/shared.pri diff --git a/examples/quick/controls/basiclayouts/basiclayouts.pro b/examples/quick/controls/basiclayouts/basiclayouts.pro index 144ad0fe4..a5586fe62 100644 --- a/examples/quick/controls/basiclayouts/basiclayouts.pro +++ b/examples/quick/controls/basiclayouts/basiclayouts.pro @@ -3,6 +3,7 @@ TARGET = basiclayouts !android: !ios: !blackberry: qtHaveModule(widgets): QT += widgets include(src/src.pri) +include(../shared/shared.pri) OTHER_FILES += \ main.qml diff --git a/examples/quick/controls/basiclayouts/main.qml b/examples/quick/controls/basiclayouts/main.qml index fe573aa6c..18883d9a9 100644 --- a/examples/quick/controls/basiclayouts/main.qml +++ b/examples/quick/controls/basiclayouts/main.qml @@ -47,6 +47,7 @@ import QtQuick.Controls 1.1 import QtQuick.Layouts 1.0 ApplicationWindow { + visible: true title: "Basic layouts" property int margin: 11 width: mainLayout.implicitWidth + 2 * margin diff --git a/examples/quick/controls/basiclayouts/src/main.cpp b/examples/quick/controls/basiclayouts/src/main.cpp index 57ba57e35..a757f4853 100644 --- a/examples/quick/controls/basiclayouts/src/main.cpp +++ b/examples/quick/controls/basiclayouts/src/main.cpp @@ -38,5 +38,12 @@ ** ****************************************************************************/ -#include "../../shared/qt_quick_controls_examplemain.h" -QT_QUICK_CONTROLS_EXAMPLE_MAIN(qrc:/main.qml) +#include "qtquickcontrolsapplication.h" +#include <QtQml/QQmlApplicationEngine> + +int main(int argc, char *argv[]) +{ + QtQuickControlsApplication app(argc, argv); + QQmlApplicationEngine engine(QUrl("qrc:/main.qml")); + return app.exec(); +} diff --git a/examples/quick/controls/gallery/gallery.pro b/examples/quick/controls/gallery/gallery.pro index 09358658d..68ed0c161 100644 --- a/examples/quick/controls/gallery/gallery.pro +++ b/examples/quick/controls/gallery/gallery.pro @@ -3,6 +3,7 @@ TARGET = gallery !android: !ios: !blackberry: qtHaveModule(widgets): QT += widgets include(src/src.pri) +include(../shared/shared.pri) OTHER_FILES += \ main.qml \ diff --git a/examples/quick/controls/gallery/main.qml b/examples/quick/controls/gallery/main.qml index 43b6776ea..61e77ef81 100644 --- a/examples/quick/controls/gallery/main.qml +++ b/examples/quick/controls/gallery/main.qml @@ -49,6 +49,7 @@ import QtQuick.Dialogs 1.0 import "content" ApplicationWindow { + visible: true title: "Component Gallery" width: 640 diff --git a/examples/quick/controls/gallery/src/main.cpp b/examples/quick/controls/gallery/src/main.cpp index 57ba57e35..a757f4853 100644 --- a/examples/quick/controls/gallery/src/main.cpp +++ b/examples/quick/controls/gallery/src/main.cpp @@ -38,5 +38,12 @@ ** ****************************************************************************/ -#include "../../shared/qt_quick_controls_examplemain.h" -QT_QUICK_CONTROLS_EXAMPLE_MAIN(qrc:/main.qml) +#include "qtquickcontrolsapplication.h" +#include <QtQml/QQmlApplicationEngine> + +int main(int argc, char *argv[]) +{ + QtQuickControlsApplication app(argc, argv); + QQmlApplicationEngine engine(QUrl("qrc:/main.qml")); + return app.exec(); +} diff --git a/examples/quick/controls/shared/qt_quick_controls_examplemain.h b/examples/quick/controls/shared/qtquickcontrolsapplication.h similarity index 73% rename from examples/quick/controls/shared/qt_quick_controls_examplemain.h rename to examples/quick/controls/shared/qtquickcontrolsapplication.h index 7f73a6d18..171de6c67 100644 --- a/examples/quick/controls/shared/qt_quick_controls_examplemain.h +++ b/examples/quick/controls/shared/qtquickcontrolsapplication.h @@ -39,12 +39,8 @@ ** ****************************************************************************/ -#ifndef QT_QUICK_CONTROLS_EXAMPLEMAIN_H -#define QT_QUICK_CONTROLS_EXAMPLEMAIN_H - -#include <QtQml> -#include <QtQuick/QQuickView> -#include <QtCore/QString> +#ifndef QTQUICKCONTROLSAPPLICATION_H +#define QTQUICKCONTROLSAPPLICATION_H #ifdef QT_WIDGETS_LIB #include <QtWidgets/QApplication> @@ -55,26 +51,11 @@ QT_BEGIN_NAMESPACE #ifdef QT_WIDGETS_LIB -#define Application QApplication +#define QtQuickControlsApplication QApplication #else -#define Application QGuiApplication +#define QtQuickControlsApplication QGuiApplication #endif -#define QT_QUICK_CONTROLS_EXAMPLE_MAIN(url) \ - int main(int argc, char *argv[]) \ - { \ - Application app(argc, argv); \ - QQmlApplicationEngine engine(QUrl(#url)); \ - QObject *topLevel = engine.rootObjects().value(0); \ - QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel); \ - if ( !window ) { \ - qWarning("Error: Your root item has to be a Window."); \ - return -1; \ - } \ - window->show(); \ - return app.exec(); \ - } - QT_END_NAMESPACE -#endif // QT_QUICK_CONTROLS_EXAMPLEMAIN_H +#endif // QTQUICKCONTROLSAPPLICATION_H diff --git a/examples/quick/controls/shared/shared.pri b/examples/quick/controls/shared/shared.pri new file mode 100644 index 000000000..8ca347371 --- /dev/null +++ b/examples/quick/controls/shared/shared.pri @@ -0,0 +1,4 @@ +INCLUDEPATH += $$PWD + +SOURCES += \ + $$PWD/qtquickcontrolsapplication.h diff --git a/examples/quick/controls/splitview/main.qml b/examples/quick/controls/splitview/main.qml index 992607d62..395188ce7 100644 --- a/examples/quick/controls/splitview/main.qml +++ b/examples/quick/controls/splitview/main.qml @@ -47,6 +47,7 @@ import QtQuick.Controls 1.1 import QtQuick.Layouts 1.0 ApplicationWindow { + visible: true width: 600 height: 400 diff --git a/examples/quick/controls/splitview/splitview.pro b/examples/quick/controls/splitview/splitview.pro index 066e578c3..11c55f745 100644 --- a/examples/quick/controls/splitview/splitview.pro +++ b/examples/quick/controls/splitview/splitview.pro @@ -3,6 +3,7 @@ TARGET = splitview !android: !ios: !blackberry: qtHaveModule(widgets): QT += widgets include(src/src.pri) +include(../shared/shared.pri) OTHER_FILES += \ main.qml diff --git a/examples/quick/controls/splitview/src/main.cpp b/examples/quick/controls/splitview/src/main.cpp index 57ba57e35..a757f4853 100644 --- a/examples/quick/controls/splitview/src/main.cpp +++ b/examples/quick/controls/splitview/src/main.cpp @@ -38,5 +38,12 @@ ** ****************************************************************************/ -#include "../../shared/qt_quick_controls_examplemain.h" -QT_QUICK_CONTROLS_EXAMPLE_MAIN(qrc:/main.qml) +#include "qtquickcontrolsapplication.h" +#include <QtQml/QQmlApplicationEngine> + +int main(int argc, char *argv[]) +{ + QtQuickControlsApplication app(argc, argv); + QQmlApplicationEngine engine(QUrl("qrc:/main.qml")); + return app.exec(); +} diff --git a/examples/quick/controls/tableview/main.qml b/examples/quick/controls/tableview/main.qml index 37ab383ee..e613e42ec 100644 --- a/examples/quick/controls/tableview/main.qml +++ b/examples/quick/controls/tableview/main.qml @@ -48,7 +48,7 @@ import QtQuick.Controls 1.1 import QtQuick.XmlListModel 2.0 Window { - + visible: true width: 538 + frame.margins * 2 height: 360 + frame.margins * 2 diff --git a/examples/quick/controls/tableview/src/main.cpp b/examples/quick/controls/tableview/src/main.cpp index 57ba57e35..a757f4853 100644 --- a/examples/quick/controls/tableview/src/main.cpp +++ b/examples/quick/controls/tableview/src/main.cpp @@ -38,5 +38,12 @@ ** ****************************************************************************/ -#include "../../shared/qt_quick_controls_examplemain.h" -QT_QUICK_CONTROLS_EXAMPLE_MAIN(qrc:/main.qml) +#include "qtquickcontrolsapplication.h" +#include <QtQml/QQmlApplicationEngine> + +int main(int argc, char *argv[]) +{ + QtQuickControlsApplication app(argc, argv); + QQmlApplicationEngine engine(QUrl("qrc:/main.qml")); + return app.exec(); +} diff --git a/examples/quick/controls/tableview/tableview.pro b/examples/quick/controls/tableview/tableview.pro index f3d7d7e6d..7238d007b 100644 --- a/examples/quick/controls/tableview/tableview.pro +++ b/examples/quick/controls/tableview/tableview.pro @@ -3,6 +3,7 @@ TARGET = tableview !android: !ios: !blackberry: qtHaveModule(widgets): QT += widgets include(src/src.pri) +include(../shared/shared.pri) OTHER_FILES += \ main.qml diff --git a/examples/quick/controls/texteditor/qml/main.qml b/examples/quick/controls/texteditor/qml/main.qml index 50cbed565..89120094d 100644 --- a/examples/quick/controls/texteditor/qml/main.qml +++ b/examples/quick/controls/texteditor/qml/main.qml @@ -46,6 +46,7 @@ import QtQuick.Window 2.1 import org.qtproject.example 1.0 ApplicationWindow { + visible: true width: 640 height: 480 minimumWidth: 400 diff --git a/examples/quick/controls/texteditor/src/main.cpp b/examples/quick/controls/texteditor/src/main.cpp index a096f70d1..ea7d22de3 100644 --- a/examples/quick/controls/texteditor/src/main.cpp +++ b/examples/quick/controls/texteditor/src/main.cpp @@ -38,32 +38,14 @@ ** ****************************************************************************/ -#ifdef QT_WIDGETS_LIB -#include <QtWidgets/QApplication> -#else -#include <QtGui/QGuiApplication> -#endif -#include <QtQml> -#include <QtQuick/QQuickView> -#include <QtCore/QString> - +#include "qtquickcontrolsapplication.h" #include "documenthandler.h" +#include <QtQml/QQmlApplicationEngine> int main(int argc, char *argv[]) { -#ifdef QT_WIDGETS_LIB - QApplication app(argc, argv); -#else - QGuiApplication app(argc, argv); -#endif + QtQuickControlsApplication app(argc, argv); qmlRegisterType<DocumentHandler>("org.qtproject.example", 1, 0, "DocumentHandler"); QQmlApplicationEngine engine(QUrl("qrc:/qml/main.qml")); - QObject *topLevel = engine.rootObjects().value(0); - QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel); - if ( !window ) { - qWarning("Error: Your root item has to be a Window."); - return -1; - } - window->show(); return app.exec(); } diff --git a/examples/quick/controls/texteditor/texteditor.pro b/examples/quick/controls/texteditor/texteditor.pro index d9b6c8a33..06e669d91 100644 --- a/examples/quick/controls/texteditor/texteditor.pro +++ b/examples/quick/controls/texteditor/texteditor.pro @@ -3,6 +3,7 @@ TARGET = texteditor !android: !ios: !blackberry: qtHaveModule(widgets): QT += widgets include(src/src.pri) +include(../shared/shared.pri) OTHER_FILES += \ qml/main.qml \ diff --git a/examples/quick/controls/touch/main.qml b/examples/quick/controls/touch/main.qml index a6de8910e..ab5d534f7 100644 --- a/examples/quick/controls/touch/main.qml +++ b/examples/quick/controls/touch/main.qml @@ -43,6 +43,7 @@ import QtQuick.Controls 1.1 import "content" ApplicationWindow { + visible: true width: 800 height: 1280 diff --git a/examples/quick/controls/touch/src/main.cpp b/examples/quick/controls/touch/src/main.cpp index 57ba57e35..a757f4853 100644 --- a/examples/quick/controls/touch/src/main.cpp +++ b/examples/quick/controls/touch/src/main.cpp @@ -38,5 +38,12 @@ ** ****************************************************************************/ -#include "../../shared/qt_quick_controls_examplemain.h" -QT_QUICK_CONTROLS_EXAMPLE_MAIN(qrc:/main.qml) +#include "qtquickcontrolsapplication.h" +#include <QtQml/QQmlApplicationEngine> + +int main(int argc, char *argv[]) +{ + QtQuickControlsApplication app(argc, argv); + QQmlApplicationEngine engine(QUrl("qrc:/main.qml")); + return app.exec(); +} diff --git a/examples/quick/controls/touch/touch.pro b/examples/quick/controls/touch/touch.pro index 1e5d12f3c..6afca1404 100644 --- a/examples/quick/controls/touch/touch.pro +++ b/examples/quick/controls/touch/touch.pro @@ -3,6 +3,7 @@ TARGET = touch !android: !ios: !blackberry: qtHaveModule(widgets): QT += widgets include(src/src.pri) +include(../shared/shared.pri) OTHER_FILES += \ main.qml \ -- GitLab