1

My program has two modes GUI(on qml) and no GUI(command line).What code I must write for changing mode by passing argument in cmd for example if I pass nameofapp.exe -no-gui must be work no-gui version for simple qt/c++ application we have this code

QCoreApplication* createApplication(int &argc, char *argv[])
{
    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<QCoreApplication> app(createApplication(argc, argv));

    if (qobject_cast<QApplication *>(app.data())) {
       // start GUI version...
    } else {
       // start non-GUI version...
    }

    return app->exec();
}

It's from documentation,I want such of this for qml OS is windows 7 ultimate but I don't think that it's depends on OS

Vardan95
  • 602
  • 1
  • 7
  • 15
  • What operating system? – Ben Aug 22 '14 at 10:20
  • 3
    If you want to target Windows keep in mind that Windows does not really support dual mode applications. http://stackoverflow.com/questions/3360548/console-output-in-a-qt-gui-app – Programmer Aug 22 '14 at 11:28
  • 2
    So what actually is your problem? That example code seems clear enough to me. – hyde Aug 27 '14 at 21:11

1 Answers1

1

You need at least one top level QObject per application type which will "make things happen" in the main event loop.

Assuming that you have made a clear separation of concern between the view and the backend (eg MVC), you will have 3 classes (which are QObject) :

  • MyAppBackend which does the actual work
  • MyGUIMainWindow which manage GUI input\outpout.
  • MyCLIMainObject which manage command line input\outpout

Each one should have a method to start doing work. For instance just showing a widget is how simple Qt apps start.

int main()
{
  QApplication app;

  MyGUIMainWindow window;
  window.show();

  return app.exec();
}

window.show() will queue an event which will be processed when app.exec(). is executed.

You need to do the same with a function which either post an event or emit a signal.

int main(int argc, char* argv[])
{
    QScopedPointer<QCoreApplication> app(createApplication(argc, argv));

    MyAppBackend backend;
    backend.startDeffered();

    if (qobject_cast<QApplication *>(app.data())) {
       // start GUI version...
       MyGUIMainWindow* window = new MyGUIMainWindow(0,...);
       window->show();
    } else {
       // start non-GUI version...
       MyCLIMainObject* cliobj = new MyCLIMainObject(0,...);
       cliobj->showCLi();
    }

    return app->exec();
}
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • I will try this,but I wrote code like this without classes,just run qml engine in GUI version of if,but program didn't work.this kind of code workes only for just qt (not qml) – Vardan95 Aug 22 '14 at 17:29
  • If your code only works in console mode, you should try removing the console functionality altogether and just implement the GUI version. If the gui version does not work, fix it, then try again. btw: for qml-only applications you don't need QApplication but QGUIApplication, this is not your problem though. – Teimpz Jan 21 '16 at 22:36