Qml gui and nogui mode[solved]



  • hi guys,my program has two modes GUI(on qml) and non 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(&#41;;
    

    }@

    It's from documentation,I want such of this for qml


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    The most simple way is to create a default qml application and copy it's main content (except the QApplication creation) in place of // start GUI version



  • [quote author="SGaist" date="1408743674"]Hi and welcome to devnet,

    The most simple way is to create a default qml application and copy it's main content (except the QApplication creation) in place of // start GUI version[/quote]

    You mean kind of this?
    @QScopedPointer<QCoreApplication> app(createApplication(argc, argv));
    if (qobject_cast<QApplication *>(app.data())) {
    qmlRegisterType<CoreAdapter>("conv.core", 1, 0, "CoreAdapter");
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    } else {
    // start non-GUI version...
    }
    return app->exec();@


  • Lifetime Qt Champion

    Did you mean in gui mode ?



  • Unfortunately, I wrote wrong,sorry for it. but this code does not work


  • Lifetime Qt Champion

    You'll have to be more specific. "Does not work" doesn't really say what is currently happening. What should you be getting ? What are you getting ? What parameters are you passing to your application ?



  • ok,in cmd I wrote app-name.exe -no-gui,program started,but not showed,it's just ran in process(Task Manager).The same with -gui parameter


  • Lifetime Qt Champion

    -no-gui implies that there will be no gui so there's nothing to be shown.

    As for a "gui" run, your QQmlApplicationEngine goes out of scope before you reach app->exec() so nothing will be shown either.



  • [quote author="SGaist" date="1408905527"]-no-gui implies that there will be no gui so there's nothing to be shown.

    As for a "gui" run, your QQmlApplicationEngine goes out of scope before you reach app->exec() so nothing will be shown either.[/quote]

    but for nu-gui I have function,and I want to execute that function(it's a simple c++ console app, wich workes for non qml version)

    and for "gui" I understand that,and I apologise for bad code :),but the result is same in this case too
    @QScopedPointer<QCoreApplication> app(createApplication(argc, argv));
    if (qobject_cast<QApplication *>(app.data())) {
    qmlRegisterType<CoreAdapter>("conv.core", 1, 0, "CoreAdapter");
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    return app->exec();
    } else {
    // start non-GUI version...
    return app->exec();
    }@


  • Lifetime Qt Champion

    If you replace your main content with

    @
    QApplication app(argc, argv);
    qmlRegisterType<CoreAdapter>("conv.core", 1, 0, "CoreAdapter");
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    return app.exec();
    @

    is it working ? If not, then first ensure you have it working before adding your gui/non-gui code



  • It workes only from Qt Creator,but from debug/release folders it's doesn't run
    I put some .dll files into that folder,but it didn't help.I get this error
    !http://s3.postimg.org/hwu6q9jer/qtprob.jpg(Error)!
    and dll files !http://s3.postimg.org/werdy9apv/dll_s.jpg(dll)!


  • Lifetime Qt Champion

    If you want to run it in the explorer then you need to follow the same step as if you were deploying it on another computer.

    Follow this fine "wiki entry":http://qt-project.org/wiki/Deploy_an_Application_on_Windows



  • Thanks very much,you really helped me :)


  • Lifetime Qt Champion

    You're welcome !

    Since you have it running now, please update the thread title prepending [solved] so that other forum users may know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.