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



  • 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.


  • Moderators

    @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.



  • @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("|")));
    


  • 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


  • Moderators


  • Moderators

    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().



  • @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.


  • Lifetime Qt Champion

    Hi,

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



  • 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.


  • Lifetime Qt Champion

    @JonB good point, I misread that line.



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



  • @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.



  • @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.


  • Lifetime Qt Champion

    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.



  • @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.



  • @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 .



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.