Qt application with optional gui

北战南征 提交于 2019-12-23 09:36:39

问题


I am going to write program using Qt for some image processing and I want it to be able to run in non-gui mode (daemon mode?). I'm inspired by VLC player, which is "typically" GUI program, where you can configure it using GUI, but you can also run it in non-gui option when it runs without GUI. Then it uses some configuration file created in GUI mode.

Question is how should be such a program design? Should be some program core, which is GUI independent and depending on options it is being connected with GUI interface?


回答1:


Yes, you could use a "headless" or "gui" option for the binary using QCommandLineParser. Note that it is only available from 5.3, but the migration path is pretty smooth within the major series if you still do not use that.

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

Build and Run

qmake && make && ./main
qmake && make && ./main --gui

Usage

    Usage: ./main [options]
My program

Options:
  -h, --help     Displays this help.
  -v, --version  Displays version information.
  --gui          Running it via GUI.



回答2:


You can pass an argument to your application when starting to show in gui or non-gui modes. For example if you pass -non-gui parameter when running in command line then the application should not show the main window and it should do some other stuff :

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    MainWindow w;

    bool GUIMode=true;

    int num = qApp->argc() ;
    for ( int i = 0; i < num; i++ )
    {
        QString s = qApp->argv()[i] ;
        if ( s.startsWith( "-non-gui" ) )
            GUIMode = false;
    }

    if(GUIMode)
    {
         w.show();
    }
    else
    {
        //start some non gui functions
    }

    return a.exec();
}



回答3:


The example by lpapp above didn't work for me, as I got

qt.qpa.screen: QXcbConnection: Could not connect to display localhost:10.0
Could not connect to any X display.

when running without an X display (any value for DISPLAY, not just localhost:10.0).

There was a workaround - export QT_QPA_PLATFORM='offscreen' - but that's not a command line option, your user is expected to do it, which isn't nice.

So, following posting a question here, further research lead me to the following QT5 document that explains the "approved" way to start up with or without a GUI depending on command line options:

https://doc.qt.io/qt-5/qapplication.html#details

However, your mileage may vary. The example there didn't "just work" for me, either!

I had to use the command line arg to then choose one of two methods to run. Each method created its own app object (QCoreApplication for headless, QApplication for GUI, as the docs show) and then running the app.

It may be because I'm working with "mostly Qt 4" code and compiling on Qt 5 that things are being a bit odd but this method now works, so I've not investigated further.

-- Peter



来源:https://stackoverflow.com/questions/23844867/qt-application-with-optional-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!