QApplication docs: restore console mode -snippet
authorJ-P Nurmi <jpnurmi@digia.com>
Mon, 10 Dec 2012 13:09:55 +0000 (14:09 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Tue, 11 Dec 2012 17:34:19 +0000 (18:34 +0100)
Change-Id: I1f8ce949ae660a209a2092a2863d5c25e2908004
Reviewed-by: Jerome Pasion <jerome.pasion@digia.com>
src/widgets/doc/snippets/code/src_gui_kernel_qapplication.cpp
src/widgets/kernel/qapplication.cpp

index aa56207..b4b1eb5 100644 (file)
 ****************************************************************************/
 
 //! [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]
 
index 80912ff..4922e2e 100644 (file)
@@ -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.