d21*_*d3q 6 c++ qt qtgui qtcore qapplication
我将使用Qt编写程序进行一些图像处理,我希望它能够以非gui模式运行(守护进程模式?).我受到了VLC播放器的启发,这是一个"典型"的GUI程序,您可以使用GUI对其进行配置,但是non-gui当它在没有GUI的情况下运行时,您也可以在选项中运行它.然后它使用在GUI模式下创建的一些配置文件.
问题是这样的程序设计应该怎样?应该是一些程序核心,它是独立于GUI的,并且取决于它与GUI接口连接的选项?
是的,你可以使用QCommandLineParser为二进制文件使用"无头"或"gui"选项.请注意,它仅在5.3中可用,但如果您仍然不使用它,则主要系列中的迁移路径非常流畅.
#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();
}
Run Code Online (Sandbox Code Playgroud)
TEMPLATE = app
TARGET = main
QT += widgets
SOURCES += main.cpp
Run Code Online (Sandbox Code Playgroud)
qmake && make && ./main
qmake && make && ./main --gui
Run Code Online (Sandbox Code Playgroud)
Usage: ./main [options]
My program
Options:
-h, --help Displays this help.
-v, --version Displays version information.
--gui Running it via GUI.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4917 次 |
| 最近记录: |