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. About double quotes in QProcess and display cmd.exe when debug

About double quotes in QProcess and display cmd.exe when debug

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 5 Posters 5.9k Views
  • 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.
  • E Offline
    E Offline
    Ehehe
    wrote on last edited by
    #1

    I have two questions and have searched in internet but not solve.

    • #1 - Environment: Win7, Qt 5.10.0, Qt Creator 4.5.0(Community), WinGW 5.3.0
      I want to use Qt to creat a GUI for part of ffmpeg features. In Windows cmd.exe I can use this command to merge two mp3 files:
    > ffmpeg.exe -i "concat:C:/Users/ehehe/Desktop/file1.mp3|C:/Users/ehehe/Desktop/file2.mp3" -acodec copy C:/Users/ehehe/Desktop/output.mp3
    

    In Qt program, I use QProcess to do same thing:

    QStringList fileList;
    fileList.append("C:/Users/ehehe/Desktop/file1.mp3");
    fileList.append("C:/Users/ehehe/Desktop/file2.mp3");
    QString savePath = "C:/Users/ehehe/Desktop/output.mp3";
    
    QProcess *cmd = new QProcess(this);
    QString program = "ffmpeg.exe";
    QStringList argvList;
    argvList.append("-i");
    argvList.append(QString("\"concat:%1\"").arg(fileList.join("|")));
    argvList.append("-acodec");
    argvList.append("copy");
    argvList.append(savePath);
    cmd->start(program,argvList);
    

    But do nothing. If I change a command which has no double quotes, same method, it will be ok. Is there anything wrong in QString(""concat:%1"").arg(fileList.join("|"))?

    • #2 - I want to see what have been sent to and respond in cmd.exe. Add this command in *.pro is useless:
    CONFIG += console
    

    How to display cmd.exe when program debug.

    Thanks.

    sierdzioS JonBJ 2 Replies Last reply
    0
    • E Ehehe

      I have two questions and have searched in internet but not solve.

      • #1 - Environment: Win7, Qt 5.10.0, Qt Creator 4.5.0(Community), WinGW 5.3.0
        I want to use Qt to creat a GUI for part of ffmpeg features. In Windows cmd.exe I can use this command to merge two mp3 files:
      > ffmpeg.exe -i "concat:C:/Users/ehehe/Desktop/file1.mp3|C:/Users/ehehe/Desktop/file2.mp3" -acodec copy C:/Users/ehehe/Desktop/output.mp3
      

      In Qt program, I use QProcess to do same thing:

      QStringList fileList;
      fileList.append("C:/Users/ehehe/Desktop/file1.mp3");
      fileList.append("C:/Users/ehehe/Desktop/file2.mp3");
      QString savePath = "C:/Users/ehehe/Desktop/output.mp3";
      
      QProcess *cmd = new QProcess(this);
      QString program = "ffmpeg.exe";
      QStringList argvList;
      argvList.append("-i");
      argvList.append(QString("\"concat:%1\"").arg(fileList.join("|")));
      argvList.append("-acodec");
      argvList.append("copy");
      argvList.append(savePath);
      cmd->start(program,argvList);
      

      But do nothing. If I change a command which has no double quotes, same method, it will be ok. Is there anything wrong in QString(""concat:%1"").arg(fileList.join("|"))?

      • #2 - I want to see what have been sent to and respond in cmd.exe. Add this command in *.pro is useless:
      CONFIG += console
      

      How to display cmd.exe when program debug.

      Thanks.

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @Ehehe said in About double quotes in QProcess and display cmd.exe when debug:

      But do nothing. If I change a command which has no double quotes, same method, it will be ok. Is there anything wrong in QString(""concat:%1"").arg(fileList.join("|"))?

      QProcess adds quotes automatically when they are needed. See the Windows note in the documentation of QProcess.

      #2 - I want to see what have been sent to and respond in cmd.exe

      Tick the "Run in terminal" checkbox in Qt Creator - it's in Project/Run menu.

      (Z(:^

      1 Reply Last reply
      2
      • E Ehehe

        I have two questions and have searched in internet but not solve.

        • #1 - Environment: Win7, Qt 5.10.0, Qt Creator 4.5.0(Community), WinGW 5.3.0
          I want to use Qt to creat a GUI for part of ffmpeg features. In Windows cmd.exe I can use this command to merge two mp3 files:
        > ffmpeg.exe -i "concat:C:/Users/ehehe/Desktop/file1.mp3|C:/Users/ehehe/Desktop/file2.mp3" -acodec copy C:/Users/ehehe/Desktop/output.mp3
        

        In Qt program, I use QProcess to do same thing:

        QStringList fileList;
        fileList.append("C:/Users/ehehe/Desktop/file1.mp3");
        fileList.append("C:/Users/ehehe/Desktop/file2.mp3");
        QString savePath = "C:/Users/ehehe/Desktop/output.mp3";
        
        QProcess *cmd = new QProcess(this);
        QString program = "ffmpeg.exe";
        QStringList argvList;
        argvList.append("-i");
        argvList.append(QString("\"concat:%1\"").arg(fileList.join("|")));
        argvList.append("-acodec");
        argvList.append("copy");
        argvList.append(savePath);
        cmd->start(program,argvList);
        

        But do nothing. If I change a command which has no double quotes, same method, it will be ok. Is there anything wrong in QString(""concat:%1"").arg(fileList.join("|"))?

        • #2 - I want to see what have been sent to and respond in cmd.exe. Add this command in *.pro is useless:
        CONFIG += console
        

        How to display cmd.exe when program debug.

        Thanks.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Ehehe said in About double quotes in QProcess and display cmd.exe when debug:

        argvList.append(QString("\"concat:%1\"").arg(fileList.join("|")));

        So as @sierdzio says, since your QProcess.start() call is passing parameters as a list, it expects them to be unquoted for it to do the quoting for you. So you will want:

        argvList.append(QString("concat:%1").arg(fileList.join("|")));
        
        1 Reply Last reply
        1
        • E Offline
          E Offline
          Ehehe
          wrote on last edited by Ehehe
          #4

          Firstly thanks for your answers very much. @JonB @sierdzio
          I also post the command in cmd.exe, you can see the second parameter, it is a string and has a pair of double quotes, so in Qt code I use \", but likely they do not be passed correctly, so I want see cmd.exe console.
          By the way, "Run in terminal" is enable default, I can see the black box, but nothing dispaly on it, I can not see the command in QProcess.start(). So sad. What have I done less? @sierdzio

          jsulmJ JonBJ 2 Replies Last reply
          0
          • E Ehehe

            Firstly thanks for your answers very much. @JonB @sierdzio
            I also post the command in cmd.exe, you can see the second parameter, it is a string and has a pair of double quotes, so in Qt code I use \", but likely they do not be passed correctly, so I want see cmd.exe console.
            By the way, "Run in terminal" is enable default, I can see the black box, but nothing dispaly on it, I can not see the command in QProcess.start(). So sad. What have I done less? @sierdzio

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Ehehe Did you try to check whether there was an error?
            See http://doc.qt.io/qt-5/qprocess.html#error and http://doc.qt.io/qt-5/qprocess.html#readyReadStandardError

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              To see the output of the terminal you need to set the channel mode to QProcess::ForwardedChannels.

              This will not show you the command that is run, only the output. I don't think there is a way to see it, but you can try nativeArguments() or program().

              (Z(:^

              1 Reply Last reply
              1
              • E Ehehe

                Firstly thanks for your answers very much. @JonB @sierdzio
                I also post the command in cmd.exe, you can see the second parameter, it is a string and has a pair of double quotes, so in Qt code I use \", but likely they do not be passed correctly, so I want see cmd.exe console.
                By the way, "Run in terminal" is enable default, I can see the black box, but nothing dispaly on it, I can not see the command in QProcess.start(). So sad. What have I done less? @sierdzio

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @Ehehe

                you can see the second parameter, it is a string and has a pair of double quotes, so in Qt code I use ", but likely they do not be passed correctly,

                When you put your own pair of double-quotes into the second argument passed as an argument list to QProcess::start(), Qt code decided you wanted to pass those quotes as part of the argument and so it will have put further quotes (and backslashes) around yours. That's why it would then be wrong.

                When you choose the QProcess::start() overload which accepts a list of arguments, Qt will quote each one as necessary: your job is to pass in those arguments without quotes, and let Qt do it for you. When you choose the QProcess::start() overload which accepts a single string of argument(s), it requires you to have done your own quoting, and it does not further-quote it.

                E 2 Replies Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  You should use QDir::toNativeSeparators for your file paths. Since you're on Windows, ffmpeg might not understand the forward slash notation.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  JonBJ E 2 Replies Last reply
                  0
                  • SGaistS SGaist

                    Hi,

                    You should use QDir::toNativeSeparators for your file paths. Since you're on Windows, ffmpeg might not understand the forward slash notation.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    In his particular case, OP started by showing his command worked with forward slashes outside of Qt. It would be better to use QDir::toNativeSeparators, but just so he understands why his existing code will work.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @JonB good point, I misread that line.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • SGaistS SGaist

                        Hi,

                        You should use QDir::toNativeSeparators for your file paths. Since you're on Windows, ffmpeg might not understand the forward slash notation.

                        E Offline
                        E Offline
                        Ehehe
                        wrote on last edited by Ehehe
                        #11

                        @SGaist Thanks for your reminder. In fact, I get the path through QFileDialog::getOpenFileName and getSaveFileName , so nothing about slash or backslash. :D

                        1 Reply Last reply
                        0
                        • JonBJ JonB

                          @Ehehe

                          you can see the second parameter, it is a string and has a pair of double quotes, so in Qt code I use ", but likely they do not be passed correctly,

                          When you put your own pair of double-quotes into the second argument passed as an argument list to QProcess::start(), Qt code decided you wanted to pass those quotes as part of the argument and so it will have put further quotes (and backslashes) around yours. That's why it would then be wrong.

                          When you choose the QProcess::start() overload which accepts a list of arguments, Qt will quote each one as necessary: your job is to pass in those arguments without quotes, and let Qt do it for you. When you choose the QProcess::start() overload which accepts a single string of argument(s), it requires you to have done your own quoting, and it does not further-quote it.

                          E Offline
                          E Offline
                          Ehehe
                          wrote on last edited by Ehehe
                          #12

                          @JonB Thanks very much! It's useful!! I don't know if the argument is string (QString), it will send double-quotes automatically. Live and learn.

                          1 Reply Last reply
                          0
                          • JonBJ JonB

                            @Ehehe

                            you can see the second parameter, it is a string and has a pair of double quotes, so in Qt code I use ", but likely they do not be passed correctly,

                            When you put your own pair of double-quotes into the second argument passed as an argument list to QProcess::start(), Qt code decided you wanted to pass those quotes as part of the argument and so it will have put further quotes (and backslashes) around yours. That's why it would then be wrong.

                            When you choose the QProcess::start() overload which accepts a list of arguments, Qt will quote each one as necessary: your job is to pass in those arguments without quotes, and let Qt do it for you. When you choose the QProcess::start() overload which accepts a single string of argument(s), it requires you to have done your own quoting, and it does not further-quote it.

                            E Offline
                            E Offline
                            Ehehe
                            wrote on last edited by Ehehe
                            #13

                            @JonB I have read the documentation fo QProcess carefully, and also have the same question: Why I do not need to add double-quotes when I choose the QProcess::start() overload which accepts a list of arguments?
                            I try this command in Windows cmd.exe(without double-quotes in second argument), it is useless :
                            > ffmpeg.exe -i concat:file1.mp3|file2.mp3 -acodec copy out.mp3
                            and try these code(have \") in Qt code, it is useful:

                            QProcess *cmd = new QProcess(this);
                            QString cmdArgv = QString("ffmpeg.exe -i \"concat:%1\" -acodec copy %2").arg(fileList.join("|")).arg(savePath);
                            cmd->start(cmdArgv);
                            

                            Above all, the second argument has a pair of double-quotes as part of itself. However these code is useful too!!!

                            QProcess *cmd = new QProcess(this);
                            QString program = "ffmpeg.exe";
                            QStringList argv;
                            argv.append(QString("-i"));
                            argv.append(QString("concat:%1").arg(compaxList.join("|")));
                            argv.append(QString("-acodec"));
                            argv.append(QString("copy"));
                            argv.append(savePath);
                            cmd->start(program,argv);
                            

                            This method makes all argument have double-quotes or have no double-quotes, but is useful.
                            Although my Qt program works successfully, I still can not figure out how QProcess::start() works. I hope your can explain for me, or give me any document link I will learn it by myself.
                            Thanks.

                            JonBJ 1 Reply Last reply
                            1
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Because you have the | char which is a pipe and is handled by the command line interpreter. If you don't escape that string properly like you have to do on a command line you end up with something that cannot be interpreted.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              1
                              • E Ehehe

                                @JonB I have read the documentation fo QProcess carefully, and also have the same question: Why I do not need to add double-quotes when I choose the QProcess::start() overload which accepts a list of arguments?
                                I try this command in Windows cmd.exe(without double-quotes in second argument), it is useless :
                                > ffmpeg.exe -i concat:file1.mp3|file2.mp3 -acodec copy out.mp3
                                and try these code(have \") in Qt code, it is useful:

                                QProcess *cmd = new QProcess(this);
                                QString cmdArgv = QString("ffmpeg.exe -i \"concat:%1\" -acodec copy %2").arg(fileList.join("|")).arg(savePath);
                                cmd->start(cmdArgv);
                                

                                Above all, the second argument has a pair of double-quotes as part of itself. However these code is useful too!!!

                                QProcess *cmd = new QProcess(this);
                                QString program = "ffmpeg.exe";
                                QStringList argv;
                                argv.append(QString("-i"));
                                argv.append(QString("concat:%1").arg(compaxList.join("|")));
                                argv.append(QString("-acodec"));
                                argv.append(QString("copy"));
                                argv.append(savePath);
                                cmd->start(program,argv);
                                

                                This method makes all argument have double-quotes or have no double-quotes, but is useful.
                                Although my Qt program works successfully, I still can not figure out how QProcess::start() works. I hope your can explain for me, or give me any document link I will learn it by myself.
                                Thanks.

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #15

                                @Ehehe
                                As @SGaist says.

                                And also, one more time: there are 2 different overloads for QProcess::start():

                                • Originally you used void QProcess::start(const QString &program, const QStringList &arguments, OpenMode mode = ReadWrite). Here QStringList &arguments is a list of separate arguments. So Qt code knows each one is an argument, and quotes each one for you. So you must not quote the arguments yourself.

                                • Then you use void QProcess::start(const QString &command, OpenMode mode = ReadWrite). Here const QString &command is a single string, the executable followed by any arguments. So Qt code does not know where each argument is, and so does no quoting for you. So you must quote the arguments yourself as you construct the string.

                                E 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @Ehehe
                                  As @SGaist says.

                                  And also, one more time: there are 2 different overloads for QProcess::start():

                                  • Originally you used void QProcess::start(const QString &program, const QStringList &arguments, OpenMode mode = ReadWrite). Here QStringList &arguments is a list of separate arguments. So Qt code knows each one is an argument, and quotes each one for you. So you must not quote the arguments yourself.

                                  • Then you use void QProcess::start(const QString &command, OpenMode mode = ReadWrite). Here const QString &command is a single string, the executable followed by any arguments. So Qt code does not know where each argument is, and so does no quoting for you. So you must quote the arguments yourself as you construct the string.

                                  E Offline
                                  E Offline
                                  Ehehe
                                  wrote on last edited by Ehehe
                                  #16

                                  @JonB Run this command in cmd.exe, and know little more.
                                  > ffmpeg.exe "-i" "concat:file1.mp3|file2.mp3" "-acodec" "copy" "output.mp3"

                                  void QProcess::start(const QString &program, const QStringList &arguments, OpenMode mode = ReadWrite) quotes each argument, and this style of command line is effective. In the original command line, only second argument has double-quotes, it confused me.

                                  Thanks for your patient explanation!

                                  And also thank you @SGaist .

                                  JonBJ 1 Reply Last reply
                                  0
                                  • E Ehehe

                                    @JonB Run this command in cmd.exe, and know little more.
                                    > ffmpeg.exe "-i" "concat:file1.mp3|file2.mp3" "-acodec" "copy" "output.mp3"

                                    void QProcess::start(const QString &program, const QStringList &arguments, OpenMode mode = ReadWrite) quotes each argument, and this style of command line is effective. In the original command line, only second argument has double-quotes, it confused me.

                                    Thanks for your patient explanation!

                                    And also thank you @SGaist .

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by
                                    #17

                                    @Ehehe

                                    Because you are using the "separate arguments list", Qt quotes (or may quote) each & every argument, just in case. It may look odd, but does no harm, and works.

                                    If you do not want to allow Qt to produce too many unnecessarily-quoted args, you must use the "single string all-arguments". Then you are responsible for pre-quoting, and in your example the only one which requires quoting is the "concat:file1.mp3|file2.mp3", because it contains a | character.

                                    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