From e481ea595e97456aec35e6a62d3ada62cddd0c2b Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 10 Dec 2012 14:09:55 +0100 Subject: QApplication docs: restore console mode -snippet Change-Id: I1f8ce949ae660a209a2092a2863d5c25e2908004 Reviewed-by: Jerome Pasion --- .../snippets/code/src_gui_kernel_qapplication.cpp | 30 ++++++++++++---------- src/widgets/kernel/qapplication.cpp | 9 +++++++ 2 files changed, 25 insertions(+), 14 deletions(-) (limited to 'src') 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 app(createApplication(argc, argv)); + + if (qobject_cast(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. -- cgit v1.2.3