Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to continually run QProcess while running my QML program?
Qt 6.11 is out! See what's new in the release blog

How to continually run QProcess while running my QML program?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 458 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MAthias_Va 0
    wrote on last edited by MAthias_Va 0
    #1

    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();
                }
            }
           }
          }
    
    
    
    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
      wrote on last edited by
      #2

      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
      @Community Service
      Certified Qt Specialist
      https://www.pthinks.com

      M 1 Reply Last reply
      4
      • dheerendraD dheerendra

        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.

        M Offline
        M Offline
        MAthias_Va 0
        wrote on last edited by
        #3

        @dheerendra Can you explain more with code!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved