Please nominate your Qt Champions for 2021!

How to continually run QProcess while running my QML program?

  • The problem I have in my code is, once the QML engine uplouded the QProcess stop!!

    #include <QQmlContext>
        #include <QGuiApplication>
        #include <QQmlApplicationEngine>
        #include <usbdeviceid.h>
        #include <iostream>
        #include <QProcess>
        using namespace std;
        int main(int argc, char *argv[])
            QGuiApplication app(argc, argv);
            QQmlApplicationEngine engine;
            bool scanForDevices=0;
            USBdeviceID *usbdeviceid =new USBdeviceID();
            engine.rootContext()->setContextProperty("USBdeviceID", usbdeviceid);
            QProcess OProcess;
            QString Command;    //Contains the command to be execute
            Command = "lsusb";
            while (1)
                OProcess.start(Command,QIODevice::ReadOnly); //Starts execution of command
                OProcess.waitForFinished();   //Waits for execution to complete
                QString StdOut      =   OProcess.readAllStandardOutput();  
                QString StdError    =   OProcess.readAllStandardError();  
                cout<<"\n Printing the standard output..........\n";
                bool mouse1 = StdOut.contains("ID 046d:c03e");                                                   
                bool keyBoard1 = StdOut.contains("ID 413c:1003"); 
                if (mouse1 ==1 && keyBoard1==1)
                if (scanForDevices==1)
                    // start main program
                    // revoke A signal to tell QML the correct devices are connected
                    if (engine.rootObjects().isEmpty())
                        return -1;
                    return app.exec();

  • Qt Champions 2017

    You have multiple issues in your program. Why are you starting the infinite while loop in the main ? You are not loading the qml unless your process finishes as you are looking for waitForFinished.

    Best thing is start your process based on some event e.g. Use timer.

    Then things should work fine.

  • @dheerendra Can you explain more with code!

Log in to reply