summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/widgets/doc/snippets/code/src_gui_kernel_qapplication.cpp30
-rw-r--r--src/widgets/kernel/qapplication.cpp9
2 files changed, 25 insertions, 14 deletions
diff --git a/src/widgets/doc/snippets/code/src_gui_kernel_qapplication.cpp b/src/widgets/doc/snippets/code/src_gui_kernel_qapplication.cpp
index aa562078a6..b4b1eb5a00 100644
--- a/src/widgets/doc/snippets/code/src_gui_kernel_qapplication.cpp
+++ b/src/widgets/doc/snippets/code/src_gui_kernel_qapplication.cpp
@@ -39,23 +39,25 @@
****************************************************************************/
//! [0]
-int main(int argc, char **argv)
+QCoreApplication* createApplication(int &argc, char *argv[])
{
-#ifdef Q_WS_X11
- bool useGUI = getenv("DISPLAY") != 0;
-#else
- bool useGUI = true;
-#endif
- QApplication app(argc, argv, useGUI);
-
- if (useGUI) {
- // start GUI version
- ...
+ for (int i = 1; i < argc; ++i)
+ if (!qstrcmp(argv[i], "-no-gui"))
+ return new QCoreApplication(argc, argv);
+ return new QApplication(argc, argv);
+}
+
+int main(int argc, char* argv[])
+{
+ QScopedPointer<QCoreApplication> app(createApplication(argc, argv));
+
+ if (qobject_cast<QApplication *>(app.data())) {
+ // start GUI version...
} else {
- // start non-GUI version
- ...
+ // start non-GUI version...
}
- return app.exec();
+
+ return app->exec();
}
//! [0]
diff --git a/src/widgets/kernel/qapplication.cpp b/src/widgets/kernel/qapplication.cpp
index 80912ff409..4922e2e778 100644
--- a/src/widgets/kernel/qapplication.cpp
+++ b/src/widgets/kernel/qapplication.cpp
@@ -190,6 +190,15 @@ QApplicationPrivate::~QApplicationPrivate()
any given time. For non-QWidget based Qt applications, use QGuiApplication instead,
as it does not depend on the \l QtWidgets library.
+ Some GUI applications provide a special batch mode ie. provide command line
+ arguments for executing tasks without manual intervention. In such non-GUI
+ mode, it is often sufficient to instantiate a plain QCoreApplication to
+ avoid unnecessarily initializing resources needed for a graphical user
+ interface. The following example shows how to dynamically create an
+ appropriate type of application instance:
+
+ \snippet code/src_gui_kernel_qapplication.cpp 0
+
The QApplication object is accessible through the instance() function that
returns a pointer equivalent to the global qApp pointer.