Vardan95
Vardan95

Reputation: 622

QML application

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

Upvotes: 1

Views: 801

Answers (1)

UmNyobe
UmNyobe

Reputation: 22890

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();
}

Upvotes: 1

Related Questions