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. How to parse an command line argument string into a QStringList for QProcess ?
Forum Updated to NodeBB v4.3 + New Features

How to parse an command line argument string into a QStringList for QProcess ?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 2.2k 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.
  • L Offline
    L Offline
    lansing
    wrote on 27 Feb 2021, 09:06 last edited by
    #1

    I have a command line string that I want to run with QProcess, it was used to be like this:

    QString executable = "x264.exe";
    QString arguments= " -i xxxxx -o test.mp4";
    QString commandline = executable + arguments;
    process->start(commandline);
    

    But now qt warns about this syntax being deprecated, so how do update this and turn the arguments string into QStringList?

    J 1 Reply Last reply 27 Feb 2021, 09:27
    0
    • L lansing
      27 Feb 2021, 09:06

      I have a command line string that I want to run with QProcess, it was used to be like this:

      QString executable = "x264.exe";
      QString arguments= " -i xxxxx -o test.mp4";
      QString commandline = executable + arguments;
      process->start(commandline);
      

      But now qt warns about this syntax being deprecated, so how do update this and turn the arguments string into QStringList?

      J Online
      J Online
      JonB
      wrote on 27 Feb 2021, 09:27 last edited by JonB
      #2

      @lansing
      In this case you would be far better generating the list of separate arguments which QProcess::start() wants, rather than making a string as you do now.

      QString executable = "x264.exe";
      QStringList arguments =  (QStringList() << "-i" << "xxxx" << "-o" << "test.mp4");
      process->start(executable, arguments);
      
      L 1 Reply Last reply 27 Feb 2021, 09:29
      2
      • J JonB
        27 Feb 2021, 09:27

        @lansing
        In this case you would be far better generating the list of separate arguments which QProcess::start() wants, rather than making a string as you do now.

        QString executable = "x264.exe";
        QStringList arguments =  (QStringList() << "-i" << "xxxx" << "-o" << "test.mp4");
        process->start(executable, arguments);
        
        L Offline
        L Offline
        lansing
        wrote on 27 Feb 2021, 09:29 last edited by
        #3

        @JonB

        The command line will be entered by the users.

        M 1 Reply Last reply 27 Feb 2021, 09:40
        0
        • L lansing
          27 Feb 2021, 09:29

          @JonB

          The command line will be entered by the users.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 27 Feb 2021, 09:40 last edited by
          #4

          @lansing
          Hi
          And how does the user enter them ?

          I mean via some GUI or via actual command line parameters to your app`?

          L 1 Reply Last reply 27 Feb 2021, 09:41
          0
          • M mrjj
            27 Feb 2021, 09:40

            @lansing
            Hi
            And how does the user enter them ?

            I mean via some GUI or via actual command line parameters to your app`?

            L Offline
            L Offline
            lansing
            wrote on 27 Feb 2021, 09:41 last edited by
            #5

            @mrjj

            I have a gui with a textarea for the entry

            M 1 Reply Last reply 27 Feb 2021, 09:45
            0
            • L lansing
              27 Feb 2021, 09:41

              @mrjj

              I have a gui with a textarea for the entry

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 27 Feb 2021, 09:45 last edited by
              #6

              @lansing
              ok so user type one parameter pr line or does it come
              like par1=1 par2=2 on same line ?

              So we need to find way to split it into elements and
              often we can split on space or or new line.

              L 1 Reply Last reply 27 Feb 2021, 09:52
              0
              • M mrjj
                27 Feb 2021, 09:45

                @lansing
                ok so user type one parameter pr line or does it come
                like par1=1 par2=2 on same line ?

                So we need to find way to split it into elements and
                often we can split on space or or new line.

                L Offline
                L Offline
                lansing
                wrote on 27 Feb 2021, 09:52 last edited by
                #7

                @mrjj

                The commandline will be entered by the user in one line, without the executable at the start of the line.

                -i xxxxx -o "D:\my test.mp4"

                I tried splitting it by space but it failed when I included a file path.

                M J 2 Replies Last reply 27 Feb 2021, 09:55
                0
                • L lansing
                  27 Feb 2021, 09:52

                  @mrjj

                  The commandline will be entered by the user in one line, without the executable at the start of the line.

                  -i xxxxx -o "D:\my test.mp4"

                  I tried splitting it by space but it failed when I included a file path.

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 27 Feb 2021, 09:55 last edited by
                  #8

                  @lansing
                  Why not just use multiple LineEdits?

                  Input:   [              ]
                  output:[              ]
                  

                  and then add -i and -o yourself when you build the QStringList

                  L 1 Reply Last reply 27 Feb 2021, 10:21
                  0
                  • M mrjj
                    27 Feb 2021, 09:55

                    @lansing
                    Why not just use multiple LineEdits?

                    Input:   [              ]
                    output:[              ]
                    

                    and then add -i and -o yourself when you build the QStringList

                    L Offline
                    L Offline
                    lansing
                    wrote on 27 Feb 2021, 10:21 last edited by lansing
                    #9

                    @mrjj

                    I think you're right, separating input/output file path from the argument string will resolve headache like this. Then the argument string can be easily splitted by space and put into a QStringList.

                    1 Reply Last reply
                    0
                    • Chris KawaC Online
                      Chris KawaC Online
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on 27 Feb 2021, 10:42 last edited by
                      #10

                      There's also QCommandLineParser class that can take care of all the spacing, quoting and parsing problems for you.

                      1 Reply Last reply
                      1
                      • L lansing
                        27 Feb 2021, 09:52

                        @mrjj

                        The commandline will be entered by the user in one line, without the executable at the start of the line.

                        -i xxxxx -o "D:\my test.mp4"

                        I tried splitting it by space but it failed when I included a file path.

                        J Online
                        J Online
                        JonB
                        wrote on 27 Feb 2021, 10:43 last edited by JonB
                        #11

                        @lansing said in How to parse an command line argument string into a QStringList for QProcess ?:

                        The commandline will be entered by the user in one line

                        Then instead of using the deprecated QProcess::start() which accepted a string you will want to use the new [static]QStringList QProcess::splitCommand(QStringView command), which effectively exposes the function to do the splitting into arguments that the old start() used to offer.

                        L 1 Reply Last reply 27 Feb 2021, 17:49
                        2
                        • J JonB
                          27 Feb 2021, 10:43

                          @lansing said in How to parse an command line argument string into a QStringList for QProcess ?:

                          The commandline will be entered by the user in one line

                          Then instead of using the deprecated QProcess::start() which accepted a string you will want to use the new [static]QStringList QProcess::splitCommand(QStringView command), which effectively exposes the function to do the splitting into arguments that the old start() used to offer.

                          L Offline
                          L Offline
                          lansing
                          wrote on 27 Feb 2021, 17:49 last edited by
                          #12

                          @JonB

                          Thanks it worked like magic.

                          QString executable = "x264.exe";
                          QString arguments = " -i xxxxx -o test.mp4";
                          QStringList argumentsList =  QProcess::splitCommand(arguments);
                          process->start(executable, argumentsList );
                          
                          1 Reply Last reply
                          1

                          1/12

                          27 Feb 2021, 09:06

                          • Login

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