我将使用Qt编写程序进行一些图像处理,我希望它能够以非gui模式运行(守护进程模式?).我受到VLC播放器的启发,这是一个“典型”的GUI程序,您可以使用GUI对其进行配置,但是当它在没有GUI的情况下运行时,您也可以在非gui选项中运行它.然后它使用在GUI模式下创建的一些配置文件.
问题是这样的程序设计应该怎样?应该是一些程序核心,它是独立于GUI的,并且取决于它与GUI接口连接的选项?
解决方法
是的,您可以使用
QCommandLineParser对二进制文件使用“无头”或“gui”选项.请注意,它仅在5.3中可用,但如果您仍然不使用它,则主要系列中的迁移路径非常流畅.
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
构建并运行
- qmake && make && ./main
- qmake && make && ./main --gui
- Usage: ./main [options]
- My program
- Options:
- -h,--help Displays this help.
- -v,--version Displays version information.
- --gui Running it via GUI.