Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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[])
        {
            QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
            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";
                cout<<endl<<StdOut.toStdString();
                bool mouse1 = StdOut.contains("ID 046d:c03e");                                                   
                bool keyBoard1 = StdOut.contains("ID 413c:1003"); 
    
                if (mouse1 ==1 && keyBoard1==1)
                {
                    scanForDevices=1;
                }
    
                if (scanForDevices==1)
                {
                    // start main program
                    // revoke A signal to tell QML the correct devices are connected
                    usbdeviceid->setMouse1Detected(1);
                    usbdeviceid->setkeyBoard1Detected(1);
                    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                    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