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. Passing multiple parameters to QProcess
Qt 6.11 is out! See what's new in the release blog

Passing multiple parameters to QProcess

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 1.1k Views 3 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 last edited by
    #1

    I am having an issue with passing multiple parameters to QProcess -> QStringList.

    Is there a way to verify the actual contents of
    QStringList BEFORE running QProcess start?

    PS QProcess works, verified with connect error and status , with single params, but no leading or terminating spaces.

    QStringList params;
    params << "-maximized"; // works fine by itself
    params << "-e" << "lsusb"; // QProcess starts but terminates
    //
    QP->start("xterm", params);</pre></pre>

    Axel SpoerlA 1 Reply Last reply
    0
    • Q Offline
      Q Offline
      quadbyte
      wrote on last edited by
      #2

      You can redirect standard output & error that way:

         QProcess ftp;   
      
         QObject::connect (&ftp, &QProcess::readyReadStandardOutput, [&](){
            QByteArray data = ftp.readAllStandardOutput();
            auto coutList = data.split('\n');
      
            qDebug() << coutList;
         });
         QObject::connect (&ftp, &QProcess::readyReadStandardError, [&](){
            QByteArray data = ftp.readAllStandardError();
            auto coutList = data.split('\n');
      
            qDebug() << coutList;
         });
      
         ftp.start("wget", QStringList() << "-P" << url.toString());
         return ftp.waitForFinished();
      
      JonBJ 1 Reply Last reply
      1
      • A Anonymous_Banned275

        I am having an issue with passing multiple parameters to QProcess -> QStringList.

        Is there a way to verify the actual contents of
        QStringList BEFORE running QProcess start?

        PS QProcess works, verified with connect error and status , with single params, but no leading or terminating spaces.

        QStringList params;
        params << "-maximized"; // works fine by itself
        params << "-e" << "lsusb"; // QProcess starts but terminates
        //
        QP->start("xterm", params);</pre></pre>

        Axel SpoerlA Offline
        Axel SpoerlA Offline
        Axel Spoerl
        Moderators
        wrote on last edited by
        #3

        @AnneRanch said in Passing multiple parameters to QProcess:

        Is there a way to verify the actual contents of
        QStringList BEFORE running QProcess start?

        qDebug() << params;

        Software Engineer
        The Qt Company, Oslo

        1 Reply Last reply
        2
        • Q quadbyte

          You can redirect standard output & error that way:

             QProcess ftp;   
          
             QObject::connect (&ftp, &QProcess::readyReadStandardOutput, [&](){
                QByteArray data = ftp.readAllStandardOutput();
                auto coutList = data.split('\n');
          
                qDebug() << coutList;
             });
             QObject::connect (&ftp, &QProcess::readyReadStandardError, [&](){
                QByteArray data = ftp.readAllStandardError();
                auto coutList = data.split('\n');
          
                qDebug() << coutList;
             });
          
             ftp.start("wget", QStringList() << "-P" << url.toString());
             return ftp.waitForFinished();
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @quadbyte
          I don't think OP is asking about retrieving output from the subprocess. If she were there would be no reason to run an xterm to execute the lsusb process or whatever command, she would run it directly. As discussed in other threads, you cannot retrieve output from an xterm process as it does not produce any to stdout/err etc., it handles it internally.

          @AnneRanch
          Your issue has nothing to do with multiple parameters, string lists or spaces.

          Is there a way to verify the actual contents of

          QStringList BEFORE running QProcess start?

          Of course. Since you put the parameters into a QStringList variable you can qDebug() << params;. That tells you precisely what the contents are before you pass to QProcess, no more and no less than that.

          params << "-e" << "lsusb"; // QProcess starts but terminates

          Well yes, because that is what you tell it to do with the -e argument. xterm -e whatever says: start an xterm, run just and only whatever in it and then when that completes exit the xterm. So as soon as your lsusb command finishes, which I guess is microseconds, the xterm process finishes, closes its window, and the QProcess receives finished and exit code from the xterm. Tested with xterm -e ls it is so quick you don't even see the xterm appear and disappear.

          You can see this for yourself, nothing to do with Qt or QProcess. In a terminal just run xterm -e lsusb. You should effectively see nothing (because it's so fast), and you get back to the command prompt indicating that the xterm has exited.

          If you want to see what is really happening try a long-running command with lots of output: xterm -e find / -print. See how you see all the output scroll, and when it gets to the end the xterm window closes and exits.

          I don't know what you intend to achieve via xterm -e lsusb other than than this behaviour. If you perhaps want the xterm to run the lsusb command and then stay open with its output then ask for this, it would require a different command line to xterm using the -hold option:

          // from terminal
          xterm -hold -e lsusb
          // from Qt
          params << "-hold" << "-e" << "lsusb";
          QP->start("xterm", params);
          

          This will run the lsusb and then hold the xterm window open waiting for you to manually close it. The QProcess will not finish until you do so.

          A 1 Reply Last reply
          2
          • JonBJ JonB

            @quadbyte
            I don't think OP is asking about retrieving output from the subprocess. If she were there would be no reason to run an xterm to execute the lsusb process or whatever command, she would run it directly. As discussed in other threads, you cannot retrieve output from an xterm process as it does not produce any to stdout/err etc., it handles it internally.

            @AnneRanch
            Your issue has nothing to do with multiple parameters, string lists or spaces.

            Is there a way to verify the actual contents of

            QStringList BEFORE running QProcess start?

            Of course. Since you put the parameters into a QStringList variable you can qDebug() << params;. That tells you precisely what the contents are before you pass to QProcess, no more and no less than that.

            params << "-e" << "lsusb"; // QProcess starts but terminates

            Well yes, because that is what you tell it to do with the -e argument. xterm -e whatever says: start an xterm, run just and only whatever in it and then when that completes exit the xterm. So as soon as your lsusb command finishes, which I guess is microseconds, the xterm process finishes, closes its window, and the QProcess receives finished and exit code from the xterm. Tested with xterm -e ls it is so quick you don't even see the xterm appear and disappear.

            You can see this for yourself, nothing to do with Qt or QProcess. In a terminal just run xterm -e lsusb. You should effectively see nothing (because it's so fast), and you get back to the command prompt indicating that the xterm has exited.

            If you want to see what is really happening try a long-running command with lots of output: xterm -e find / -print. See how you see all the output scroll, and when it gets to the end the xterm window closes and exits.

            I don't know what you intend to achieve via xterm -e lsusb other than than this behaviour. If you perhaps want the xterm to run the lsusb command and then stay open with its output then ask for this, it would require a different command line to xterm using the -hold option:

            // from terminal
            xterm -hold -e lsusb
            // from Qt
            params << "-hold" << "-e" << "lsusb";
            QP->start("xterm", params);
            

            This will run the lsusb and then hold the xterm window open waiting for you to manually close it. The QProcess will not finish until you do so.

            A Offline
            A Offline
            Anonymous_Banned275
            wrote on last edited by
            #5

            Partially solved.
            It helps to have understanding of QProcess and operator "<<".

            Or just paying attention what works...

            params need to have only single item...

             params << "-fa";
                params << "Monospace";
                params << "-fs";
                params <<"16";
            
            
            Pl45m4P 1 Reply Last reply
            0
            • A Anonymous_Banned275

              Partially solved.
              It helps to have understanding of QProcess and operator "<<".

              Or just paying attention what works...

              params need to have only single item...

               params << "-fa";
                  params << "Monospace";
                  params << "-fs";
                  params <<"16";
              
              
              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #6

              @AnneRanch said in Passing multiple parameters to QProcess:

              params need to have only single item...

              Not quite correct.
              params is a QStringList. A LIST of QString.
              With the << operator you direct the right side to the left (i.e. the string list).

              params need to have only single item...

              So it's more like "You can add only one after another"
              (because QProcess needs every argument as separate string)

              As you might already know / have seen from @Axel-Spoerl 's suggestion to use qDebug()

              This

              params << "-fa";
              params << "Monospace";
              params << "-fs";
              params <<"16";
              

              produces:

              • QStringList("-fa")
              • QStringList("-fa", "Monospace")
              • QStringList("-fa", "Monospace", "-fs")
              • QStringList("-fa", "Monospace", "-fs", "16")

              Each element is a QString and the next to-be-added item is appended to the end of the list.


              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
              0

              • Login

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