Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QProcess C++ syntax - passing program and arguments
Forum Updated to NodeBB v4.3 + New Features

QProcess C++ syntax - passing program and arguments

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 5 Posters 5.2k Views 2 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on 11 Sept 2020, 15:14 last edited by aha_1980
    #1

    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]

    P P 2 Replies Last reply 11 Sept 2020, 15:52
    0
    • A Anonymous_Banned275
      14 Sept 2020, 16:59

      @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
      
      C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 14 Sept 2020, 17:02 last edited by
      #12

      @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • A Anonymous_Banned275
        11 Sept 2020, 15:14

        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]

        P Offline
        P Offline
        Pablo J. Rogina
        wrote on 11 Sept 2020, 15:52 last edited by
        #2

        @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
        3
        • A Offline
          A Offline
          Anonymous_Banned275
          wrote on 11 Sept 2020, 17:18 last edited by
          #3

          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
          0
          • A Anonymous_Banned275
            11 Sept 2020, 15:14

            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]

            P Offline
            P Offline
            Pl45m4
            wrote on 11 Sept 2020, 18:10 last edited by Pl45m4 9 Nov 2020, 20:15
            #4

            @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
            5
            • A Offline
              A Offline
              Anonymous_Banned275
              wrote on 13 Sept 2020, 17:11 last edited by
              #5

              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 .

              P 1 Reply Last reply 13 Sept 2020, 17:30
              0
              • A Anonymous_Banned275
                13 Sept 2020, 17:11

                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 .

                P Offline
                P Offline
                Pl45m4
                wrote on 13 Sept 2020, 17:30 last edited by Pl45m4
                #6

                @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
                2
                • A Offline
                  A Offline
                  Anonymous_Banned275
                  wrote on 14 Sept 2020, 01:30 last edited by Anonymous_Banned275
                  #7

                  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

                  J 1 Reply Last reply 14 Sept 2020, 09:10
                  0
                  • A Anonymous_Banned275
                    14 Sept 2020, 01:30

                    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

                    J Offline
                    J Offline
                    JonB
                    wrote on 14 Sept 2020, 09:10 last edited by JonB
                    #8

                    @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 14 Sept 2020, 14:16
                    4
                    • J JonB
                      14 Sept 2020, 09:10

                      @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 Offline
                      A Offline
                      Anonymous_Banned275
                      wrote on 14 Sept 2020, 14:16 last edited by
                      #9

                      @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

                      J 1 Reply Last reply 14 Sept 2020, 15:42
                      0
                      • A Anonymous_Banned275
                        14 Sept 2020, 14:16

                        @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

                        J Offline
                        J Offline
                        JonB
                        wrote on 14 Sept 2020, 15:42 last edited by JonB
                        #10

                        @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
                        3
                        • A Offline
                          A Offline
                          Anonymous_Banned275
                          wrote on 14 Sept 2020, 16:59 last edited by
                          #11

                          @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
                          
                          C J 2 Replies Last reply 14 Sept 2020, 17:02
                          0
                          • A Anonymous_Banned275
                            14 Sept 2020, 16:59

                            @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
                            
                            C Online
                            C Online
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 14 Sept 2020, 17:02 last edited by
                            #12

                            @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            4
                            • A Anonymous_Banned275
                              14 Sept 2020, 16:59

                              @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
                              
                              J Offline
                              J Offline
                              JonB
                              wrote on 14 Sept 2020, 18:17 last edited by JonB
                              #13

                              @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
                              0
                              • A Offline
                                A Offline
                                Anonymous_Banned275
                                wrote on 14 Sept 2020, 20:03 last edited by
                                #14

                                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
                                0

                                8/14

                                14 Sept 2020, 09:10

                                • Login

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