Yes, you can use the headless or gui option for binary using QCommandLineParser . Note that it is only available with 5.3, but the migration path is pretty smooth in the main series if you are still not using this.
main.cpp
#include <QApplication> #include <QLabel> #include <QDebug> #include <QCommandLineParser> #include <QCommandLineOption> int main(int argc, char **argv) { QApplication application(argc, argv); QCommandLineParser parser; parser.setApplicationDescription("My program"); parser.addHelpOption(); parser.addVersionOption(); // A boolean option for running it via GUI (--gui) QCommandLineOption guiOption(QStringList() << "gui", "Running it via GUI."); parser.addOption(guiOption); // Process the actual command line arguments given by the user parser.process(application); QLabel label("Runninig in GUI mode"); if (parser.isSet(guiOption)) label.show(); else qDebug() << "Running in headless mode"; return application.exec(); }
main.pro
TEMPLATE = app TARGET = main QT += widgets SOURCES += main.cpp
Assembly and launch
qmake && make && ./main qmake && make && ./main --gui
Using
Usage: ./main [options] My program Options: -h, --help Displays this help. -v, --version Displays version information. --gui Running it via GUI.
lpapp source share