Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved QProcess C++ syntax - passing program and arguments

    General and Desktop
    5
    14
    1690
    Loading More Posts
    • 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.
    • A
      AnneRanch last edited by aha_1980

      I am trying to utilize QProcess - replacing

      system ("hcitool scan ");

      with

      myProcess->start(program, arguments);

      while start is defined as

      start(QString, QStringList)

      Please help me to transfer original "system call" CORRECTLY to
      QProcess -> start.

      I am hopelessly lost.

      Cheers

      [Edit aha_1980: Fixed QProgress to QProcess]

      Pablo J. Rogina Pl45m4 2 Replies Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion @AnneRanch last edited by

        @AnneRanch said in QProcess C++ syntax - passing program and arguments:

        QProcess process;
        process.start("hciconfig", QStringList() << "-a");
        
        QString stdout = process.readAllStandardOutput();
        //   ui->plainTextEdit->appendPlainText(stdout);
        QString stderr = process.readAllStandardError();
        

        QProcess is asynchron! Read the docs!

        Qt has to stay free or it will die.

        1 Reply Last reply Reply Quote 4
        • Pablo J. Rogina
          Pablo J. Rogina @AnneRanch last edited by

          @AnneRanch said in QProgress C++ syntax - passing program and arguments:

          system ("hcitool scan ");

          I guess you will be able to answer the following questions yourself from previous snippet>

          1. What's the program name?
          2. What's the argument(s)?

          so you will be able to fill in the proper values to call QProgress then

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply Reply Quote 3
          • A
            AnneRanch last edited by

            Here is my implementation,
            system("hciconfig -a"); works as expected

            process.start((QString) "system" , (QStringList) "hciconfig -a " );

            validates
            if(stdout.isEmpty())

            as empty

                std::cerr<< "String is empty " << std::endl;
            

            #ifdef TRACE
            std::cerr <<"TRACE \nSTART QProcess \nfunction "<< FUNCTION << " \nfile " << FILE<< " \nline # "<< LINE<< std::endl;
            #endif
            system("hciconfig -a");
            //#ifdef BYPASS
            QProcess process;
            process.start((QString) "system" , (QStringList) "hciconfig -a " );
            process.waitForFinished(-1); // will wait forever until finished

            QString stdout = process.readAllStandardOutput();
            

            // ui->plainTextEdit->appendPlainText(stdout);
            QString stderr = process.readAllStandardError();
            if(stdout.isEmpty())
            std::cerr<< "String is empty " << std::endl;
            ui->plainTextEdit->setPlainText("TEST"); // (QString) stderr);
            ui->plainTextEdit->appendPlainText((QString) stderr);
            ui->plainTextEdit->appendPlainText((QString) stdout);

            process.close();
            

            #ifdef TRACE
            std::cerr <<"TRACE \nEND finished QProcess \nfunction "<< FUNCTION << " \nfile " << FILE<< " \nline # "<< LINE<< std::endl;
            #endif

            1 Reply Last reply Reply Quote 0
            • Pl45m4
              Pl45m4 @AnneRanch last edited by Pl45m4

              @AnneRanch said in QProgress C++ syntax - passing program and arguments:

              while start is defined as
              start(QString, QStringList)

              That's correct.

              So what do you expect from doing

              process.start((QString) "system" , (QStringList) "hciconfig -a " );

              ?
              (hciconfig is your command, not system)

              Try (this is in terms of "how to start a QProcess" the correct way - But I don't know if your command is correct):

              // declare + define your programm string (name of program)
              QString cmd = "hciconfig"; 
              
              // declare a StringList
              QStringList args; 
              
              // Append argument "-a" to stringList
              args << "-a";
              
              // start process
              process.start(cmd, args);
              

              Edit:
              C-style casts (e.g. (QString) test) are not safe to use in C++. Better use the QString - constructors to create your string or the way, I've used above.
              process.start(QString("hciconfig"), QStringList(QString("-a")));


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply Reply Quote 5
              • A
                AnneRanch last edited by

                Here is my test implementation

                   ui->setupUi(this);
                    system ("hciconfig -a");
                    // declare + define your programm string (name of program)
                    qDebug ("declare + define your programm string (name of program ");\
                    
                    QProcess process;
                    QString cmd = "hciconfig";
                    // declare a StringList
                    QStringList args;
                    // Append argument "-a" to stringList
                    args << "-a";
                      // start process
                    process.start(cmd, args);
                    //   QProcess process;
                    //   process.start((QString) "system" , (QStringList)  "hciconfig -a " );
                    //    process.waitForFinished(-1); // will wait forever until finished
                     QString stdout = process.readAllStandardOutput();
                    //   ui->plainTextEdit->appendPlainText(stdout);
                    QString stderr = process.readAllStandardError();
                    if(stdout.isEmpty())
                    {
                        std::cerr<< "stdout String is empty " << std::endl;
                        std::cout<< "stdout String is empty " << std::endl;
                    }
                    if(stderr.isEmpty())
                    {
                        std::cerr<< "stderr String is empty " << std::endl;
                        std::cout<< "stderr String is empty " << std::endl;
                    }
                     std::cout << " Actual process output "<< stdout.toStdString()<< std::endl;
                    std::cout << " Actual process output "<< stderr.toStdString()<< std::endl;
                       stdout = "TEST DUMMY stdout output ";
                    std::cout << " Actual process output "<< stdout.toStdString()<< std::endl;
                    std::cout << " Actual process output "<< stderr.toStdString()<< std::endl;
                     //    ui->plainTextEdit->setPlainText("TEST"); // (QString) stderr);
                    //    ui->plainTextEdit->appendPlainText((QString) stderr);
                    //    ui->plainTextEdit->appendPlainText((QString) stdout);
                    
                    process.close();
                    qDebug ("declare + define your programm string (name of program ");
                    exit(53);
                    
                

                And here is the concole output

                Starting /media/z/DEV_COPY_LABEL/Qt/QT/qtconnectivity/examples/bluetooth/build-CAT_BT-Desktop-Debug/btscanner...
                hci0:	Type: BR/EDR  Bus: USB
                	BD Address: 00:15:83:15:A2:CB  ACL MTU: 672:4  SCO MTU: 48:1
                	UP RUNNING PSCAN ISCAN 
                	RX bytes:5516 acl:0 sco:0 events:84 errors:0
                	TX bytes:790 acl:0 sco:0 commands:77 errors:0
                	Features: 0xff 0x3e 0x85 0x38 0x18 0x18 0x00 0x00
                	Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 
                	Link policy: RSWITCH HOLD SNIFF 
                	Link mode: SLAVE ACCEPT 
                	Name: 'z-desktop'
                	Class: 0x1c0104
                	Service Classes: Rendering, Capturing, Object Transfer
                	Device Class: Computer, Desktop workstation
                	HCI Version: 2.0 (0x3)  Revision: 0xc5c
                	LMP Version: 2.0 (0x3)  Subversion: 0xc5c
                	Manufacturer: Cambridge Silicon Radio (10)
                
                stdout String is empty 
                stderr String is empty 
                 Actual process output 
                 Actual process output 
                 Actual process output TEST DUMMY stdout output 
                 Actual process output 
                declare + define your programm string (name of program 
                stdout String is empty 
                stderr String is empty 
                declare + define your programm string (name of program 
                /media/z/DEV_COPY_LABEL/Qt/QT/qtconnectivity/examples/bluetooth/build-CAT_BT-Desktop-Debug/btscanner exited with code 53
                

                I believe I am missing instructing the "process" to run "system" function with "hci.." as parameter.
                My syntax MUST be wrong and I really do not know how to fix it.

                Cheers .

                Pl45m4 1 Reply Last reply Reply Quote 0
                • Pl45m4
                  Pl45m4 @AnneRanch last edited by Pl45m4

                  @AnneRanch

                  hciconfig -a did exactly what it is supposed to do

                  hciconfig prints name and basic information about all the Bluetooth devices installed in the system. If hciX is given but no command is given, it prints basic information on device hciX only. Basic information is interface type, BD address, ACL MTU, SCO MTU, flags (up, init, running, raw, page scan enabled, inquiry scan enabled, inquiry, authentication enabled, encryption enabled).

                  (https://linux.die.net/man/8/hciconfig)

                  You need some more arguments / parameters. hciconfig -a just prints the whole (-a) configuration.

                  But:
                  Why you want to deal with BlueZ directly, when you can use the Qt interface, which will use BlueZ for QBluetooth (since you are on Linux)?


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  1 Reply Last reply Reply Quote 2
                  • A
                    AnneRanch last edited by AnneRanch

                    You need some more arguments / parameters. hciconfig -a just prints the whole (-a) configuration.

                    That is NOT the issue , I am using hciconfig -a as an example.
                    The issue is how to correctly implement " system " call and retrieve the response for further processing .

                    I have a reason to deal direct with blueZ , not related to this post.

                    After looking at the attached link, I have a sneaky suspicion that same apply for redirecting "system calls" using QProgress. QProgress is probably implemented by some of the methods discussed there.

                    I am learning the hard way that SOME of the "Qtx" wrappers of low level code are really not helping, especially when the implementation is not explained and one feels like it is all high level magic.

                    In this case I really only want to pass "hci..." output to file for further processing

                    In crudest pseudocode
                    hci command output redirect to file
                    by whatever method.

                    And I did picked QProgress and having an issue with passing the parameters to it.

                    THAT is the problem - the parameter passing syntax of ANY external instructions / parameters / arguments to QProgress

                    https://stackoverflow.com/questions/2655374/how-to-redirect-the-output-of-a-system-call-to-inside-the-program-in-c-c

                    JonB 1 Reply Last reply Reply Quote 0
                    • JonB
                      JonB @AnneRanch last edited by JonB

                      @AnneRanch
                      If all you want to do is execute a single, simple command, you can indeed do that via

                      QProcess process;
                      process.start("hciconfig", QStringList() << "-a");
                      

                      and then arrange to send the bytes received to some redirection file you create from your Qt program via QProcess::setStandardOutputFile(). That is probably the simplest in this case.

                      However, if you want to do "hci command output redirect to file" where the redirection is done for you, like system("hciconfig -a > file") would do for you, you need the shell to interpret and act on a symbol like >. That is actually what the C/C++ system() call does. So, e.g if you are under Linux, you would need:

                      // next line for Linux
                      process.start("/bin/sh", QStringList() << "-c" << "hciconfig -a > file");
                      
                      // or, next line for Windows
                      process.start("cmd", QStringList() << "/c" << "hciconfig -a > file");
                      

                      This is the true equivalent of what system(string) does. Note in this case the command you want executed is passed as a single argument to /bin/sh -c "command-line", with suitable quoting as necessary for the shell. Among other things, you need to follow this route if you wish to execute a command containing >, < or | redirection symbols, as well as other situations.

                      A 1 Reply Last reply Reply Quote 4
                      • A
                        AnneRanch @JonB last edited by

                        @JonB said in QProcess C++ syntax - passing program and arguments:

                        @AnneRanch
                        If all you want to do is execute a single, simple command, you can indeed do that via

                        QProcess process;
                        process.start("hciconfig", QStringList() << "-a");
                        

                        and then arrange to send the bytes received to some redirection file you create from your Qt program via QProcess::setStandardOutputFile(). That is probably the simplest in this case.

                        However, if you want to do "hci command output redirect to file" where the redirection is done for you, like system("hciconfig -a > file") would do for you, you need the shell to interpret and act on a symbol like >. That is actually what the C/C++ system() call does. So, e.g if you are under Linux, you would need:

                        // next line for Linux
                        process.start("/bin/sh", QStringList() << "-c" << "hciconfig -a > file");
                        
                        // or, next line for Windows
                        process.start("cmd", QStringList() << "/c" << "hciconfig -a > file");
                        

                        This is the true equivalent of what system(string) does. Note in this case the command you want executed is passed as a single argument to /bin/sh -c "command-line", with suitable quoting as necessary for the shell. Among other things, you need to follow this route if you wish to execute a command containing >, < or | redirection symbols, as well as other situations.

                        Sorry about mixing process and progress.

                        I am trying to VERIFY process.star(....) using process.read...

                            QProcess process;
                            process.start((QString) "system" , (QStringList)  "hciconfig -a " );
                            process.waitForFinished(-1); // will wait forever until finished
                        
                            **QString stdout = process.readAllStandardOutput();
                         //   ui->plainTextEdit->appendPlainText(stdout);
                            QString stderr = process.readAllStandardError();**
                        

                        And both stdout an stderr are empty.

                        I figured my usage of start is wrong or my read.. is wrong .
                        Perhaps I need another way to verify this process.

                        Cheers

                        JonB 1 Reply Last reply Reply Quote 0
                        • JonB
                          JonB @AnneRanch last edited by JonB

                          @AnneRanch

                          process.start((QString) "system" , (QStringList)  "hciconfig -a " );
                          

                          You usage of system as the command to execute is mistaken.

                          I showed you what will work (guessing you are Linux):

                          QProcess process;
                          process.start("hciconfig", QStringList() << "-a");
                          // or
                          process.start("/bin/sh", QStringList() << "-c" << "hciconfig -a > file");
                          

                          The second case above is (approximately, but very close) what the C/C++ library function system() does.

                          1 Reply Last reply Reply Quote 3
                          • A
                            AnneRanch last edited by

                            @JonB said in QProcess C++ syntax - passing program and arguments:

                            process.start("hciconfig", QStringList() << "-a");

                            Code :

                                qDebug ("START NEW process");
                                QProcess process;
                                process.start("hciconfig", QStringList() << "-a");
                                
                                QString stdout = process.readAllStandardOutput();
                                //   ui->plainTextEdit->appendPlainText(stdout);
                                QString stderr = process.readAllStandardError();
                                if(stdout.isEmpty())
                                {
                                    std::cerr<< "stdout String is empty " << std::endl;
                                    std::cout<< "stdout String is empty " << std::endl;
                                }
                                if(stderr.isEmpty())
                                {
                                    std::cerr<< "stderr String is empty " << std::endl;
                                    std::cout<< "stderr String is empty " << std::endl;
                                }
                                
                                std::cout << " Actual process output "<< stdout.toStdString()<< std::endl;
                                std::cout << " Actual process output "<< stderr.toStdString()<< std::endl;
                                
                                stdout = "TEST DUMMY stdout output ";
                                
                                std::cout << " Actual process output "<< stdout.toStdString()<< std::endl;
                                std::cout << " Actual process output "<< stderr.toStdString()<< std::endl;
                                
                                
                                
                                
                                
                                //    ui->plainTextEdit->setPlainText("TEST"); // (QString) stderr);
                                //    ui->plainTextEdit->appendPlainText((QString) stderr);
                                //    ui->plainTextEdit->appendPlainText((QString) stdout);
                                
                                process.close();
                                qDebug( "END  NEW process");
                            

                            Result

                            Starting /media/z/DEV_COPY_LABEL/Qt/QT/qtconnectivity/examples/bluetooth/build-CAT_BT-Desktop-Debug/btscanner...
                            START NEW process
                            **stdout String is empty 
                            stderr String is empty** 
                             Actual process output 
                             Actual process output 
                             Actual process output TEST DUMMY stdout output 
                             Actual process output 
                            TRACE 
                            START Orignal constructor 
                            function  DeviceDiscoveryDialog 
                            file ../CAT_BT_892020/device.cpp 
                            line # 91
                            stdout String is empty 
                            stderr String is empty 
                            END  NEW process
                            
                            Christian Ehrlicher JonB 2 Replies Last reply Reply Quote 0
                            • Christian Ehrlicher
                              Christian Ehrlicher Lifetime Qt Champion @AnneRanch last edited by

                              @AnneRanch said in QProcess C++ syntax - passing program and arguments:

                              QProcess process;
                              process.start("hciconfig", QStringList() << "-a");
                              
                              QString stdout = process.readAllStandardOutput();
                              //   ui->plainTextEdit->appendPlainText(stdout);
                              QString stderr = process.readAllStandardError();
                              

                              QProcess is asynchron! Read the docs!

                              Qt has to stay free or it will die.

                              1 Reply Last reply Reply Quote 4
                              • JonB
                                JonB @AnneRanch last edited by JonB

                                @AnneRanch
                                At one point earlier you had code:

                                process.waitForFinished(-1); // will wait forever until finished
                                

                                Although this is not the best way to do things, from where you are now as a one-liner: put this line immediately after process.start("hciconfig", QStringList() << "-a");, so as to get the output from the command successfully.

                                1 Reply Last reply Reply Quote 0
                                • A
                                  AnneRanch last edited by

                                  OK, finally an answer matching the question - WHAT is the correct syntax.

                                  process.start("hciconfig", QStringList() << "-a");

                                  I am not comfortable using the << operator , hence this also works:
                                  process.start("hciconfig", QStringList("-a"));

                                  Unfortunately QProcess , and others, won't work replacing this
                                  system call

                                  system("hcitool scan ");

                                  with
                                  start("hcitool" , QSstringList (" scan"));

                                  It does work as command only - "hcitool scan" outputs
                                  "Scanning..." immediately and
                                  bluetooth device info AFTER it is physically detected - few seconds later

                                  BUT this issue is now solved , thanks.

                                  1 Reply Last reply Reply Quote 0
                                  • First post
                                    Last post