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 use QProcess with setArguments() and folders with double word name?
Qt 6.11 is out! See what's new in the release blog

How to use QProcess with setArguments() and folders with double word name?

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 5 Posters 3.3k 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.
  • T TomNow99

    @SGaist Thank you for answer. But why? I use raw string

    SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #5

    @TomNow99 said in How to use QProcess with setArguments() and folders with double word name?:

    @SGaist Thank you for answer. But why? I use raw string

    But you are not building the list correctly. Each element must be separately put in the string list.

    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
    2
    • T Offline
      T Offline
      TomNow99
      wrote on last edited by
      #6

      @SGaist Yeah, but In the second example ( with arguments ) I have something like argument in argument :D So I don't know how to do this

      @mrjj Thank you for answer. I know that, but now I have to do this with cmd

      mrjjM 1 Reply Last reply
      0
      • T TomNow99

        @SGaist Yeah, but In the second example ( with arguments ) I have something like argument in argument :D So I don't know how to do this

        @mrjj Thank you for answer. I know that, but now I have to do this with cmd

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #7

        @TomNow99
        Hi string list is clever so you can just have 2 of them

        QString program = "cmd.exe";
        QStringList arguments;
        QStringList arguments2;
        arguments2 << "1" << "2";
        arguments << "C:\Users\tom\def\folder name\pqr\abc.exe" << arguments2 << "whatever" << "and more" ;
        QProcess *myProcess = new QProcess(parent);
        myProcess->start(program, arguments);

        1 Reply Last reply
        2
        • T Offline
          T Offline
          TomNow99
          wrote on last edited by
          #8

          @mrjj Hmmm. Thank you.

          Why do you have 2 QStringLists?

          Can I do:

          arguments << "C:\Users\tom\def\folder name\pqr\abc.exe" <<"1"<<"2"<< "whatever" << "and more" ;
          

          ?

          mrjjM 1 Reply Last reply
          0
          • T TomNow99

            @mrjj Hmmm. Thank you.

            Why do you have 2 QStringLists?

            Can I do:

            arguments << "C:\Users\tom\def\folder name\pqr\abc.exe" <<"1"<<"2"<< "whatever" << "and more" ;
            

            ?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #9

            @TomNow99
            Yes also like that :)

            1 Reply Last reply
            0
            • T Offline
              T Offline
              TomNow99
              wrote on last edited by TomNow99
              #10

              @mrjj Perfect. In cmd Can I do:

              cmd.exe /c "C:\Users\tom\def\folder name\pqr\abc.exe" arg1 "arg name" ?

              I don't like cmd.exe /c ""path" arg1 arg2"

              Yes, I know that execute cmd in cmd is strange option, but sometimes I need that :)

              mrjjM 1 Reply Last reply
              0
              • T TomNow99

                @mrjj Perfect. In cmd Can I do:

                cmd.exe /c "C:\Users\tom\def\folder name\pqr\abc.exe" arg1 "arg name" ?

                I don't like cmd.exe /c ""path" arg1 arg2"

                Yes, I know that execute cmd in cmd is strange option, but sometimes I need that :)

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #11

                @TomNow99
                All arguments goes in argument list as else it might not work.
                and /c is an argument so nope it might not work with .exe part.

                1 Reply Last reply
                1
                • T TomNow99

                  Hello,

                  Let there is a folder name "folder name" and the application abc.exe there is in:
                  C:\Users\tom\def\folder name\pqr\abc.exe

                  I would like to run this abc.exe from QProcess:

                  1. When I start that application with QProcess, QT add quotes to path like this:
                    "C:\Users\tom\def\folder name\pqr\abc.exe" ?

                  2. For example my abc.exe have 2 arguments:
                    arg1 - single word
                    arg name - double word

                  And I would like to run application like this: cmd.exe /c ""C:\Users\tom\def\folder name\pqr\abc.exe" arg1 "arg name""

                  How can I do this? Like this? Or is there a better solution?

                  proc->setProgram("cmd.exe");
                  proc->setArguments(QStringList()<<"/c"<<R"("C:\Users\tom\def\folder name\pqr\abc.exe" arg1 "arg name")");
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #12

                  @TomNow99
                  Why are you wanting to run this command via cmd /c, which just makes the quoting an issue?

                  Furthermore, if you insist on doing that, I would have a very careful read of the output from cmd /? where it discusses its quoting rules in this case.

                  1 Reply Last reply
                  1
                  • T Offline
                    T Offline
                    TomNow99
                    wrote on last edited by TomNow99
                    #13

                    @JonB @SGaist @mrjj
                    Of course I can change cmd command and don't use "cmd /c", but I would like to know how to add Arguments, when some application need it in the form:

                    app.exe ""arg 1" "arg 2" "arg 3""

                    arg 1, arg 2 and arg 3 are double words.

                    Now I have 2 apps:

                    The first one (C:\Users\tom\Desktop\qprocess Args\qprocessZArgs.exe) which create file:

                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                        if(argc==3)
                        {
                            QFile file(argv[1]);
                            file.open(QIODevice::WriteOnly);
                            file.write(argv[2]);
                            file.close();
                        }
                        
                        exit(0);
                        return a.exec();
                    }
                    

                    And the second one which execute the first one:

                        proc = new QProcess;
                        proc->setProgram(R"(cmd.exe)");
                        QStringList list;
                        list<<R"(C:\Users\tom\Desktop\qprocess Args\qprocessZArgs.exe)"<<R"(C:\Users\tom\Desktop\folder name\that.txt)"<<R"(It works!)";
                    
                        proc->setArguments(QStringList()<<"/C"<<list);
                        proc->start();
                    

                    But it doesn't work ( no create file ).

                    JonBJ S 2 Replies Last reply
                    -1
                    • T TomNow99

                      @JonB @SGaist @mrjj
                      Of course I can change cmd command and don't use "cmd /c", but I would like to know how to add Arguments, when some application need it in the form:

                      app.exe ""arg 1" "arg 2" "arg 3""

                      arg 1, arg 2 and arg 3 are double words.

                      Now I have 2 apps:

                      The first one (C:\Users\tom\Desktop\qprocess Args\qprocessZArgs.exe) which create file:

                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                          if(argc==3)
                          {
                              QFile file(argv[1]);
                              file.open(QIODevice::WriteOnly);
                              file.write(argv[2]);
                              file.close();
                          }
                          
                          exit(0);
                          return a.exec();
                      }
                      

                      And the second one which execute the first one:

                          proc = new QProcess;
                          proc->setProgram(R"(cmd.exe)");
                          QStringList list;
                          list<<R"(C:\Users\tom\Desktop\qprocess Args\qprocessZArgs.exe)"<<R"(C:\Users\tom\Desktop\folder name\that.txt)"<<R"(It works!)";
                      
                          proc->setArguments(QStringList()<<"/C"<<list);
                          proc->start();
                      

                      But it doesn't work ( no create file ).

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

                      @TomNow99 said in How to use QProcess with setArguments() and folders with double word name?:

                      I give up!

                      Of course I can change cmd command and don't use "cmd /c", but I would like to know how to add Arguments, when some application need it in the form:
                      app.exe ""arg 1" "arg 2" "arg 3""

                      So your app takes quoted arguments. That's fine. But what does this have to do with wanting to run it via cmd /c??

                      proc->setArguments(QStringList()<<"/C"<<list);
                      

                      I don't know that's right, to pass a list as a single element in the new list.

                      But it doesn't work ( no create file ).

                      For goodness sake, make the first program print out whatever it receives as its arguments, so that neither you nor we have to guess what it's getting! This is just standard development debugging. It is possible it does not receive the arguments you think it does....

                      1 Reply Last reply
                      1
                      • T TomNow99

                        @JonB @SGaist @mrjj
                        Of course I can change cmd command and don't use "cmd /c", but I would like to know how to add Arguments, when some application need it in the form:

                        app.exe ""arg 1" "arg 2" "arg 3""

                        arg 1, arg 2 and arg 3 are double words.

                        Now I have 2 apps:

                        The first one (C:\Users\tom\Desktop\qprocess Args\qprocessZArgs.exe) which create file:

                        int main(int argc, char *argv[])
                        {
                            QApplication a(argc, argv);
                            if(argc==3)
                            {
                                QFile file(argv[1]);
                                file.open(QIODevice::WriteOnly);
                                file.write(argv[2]);
                                file.close();
                            }
                            
                            exit(0);
                            return a.exec();
                        }
                        

                        And the second one which execute the first one:

                            proc = new QProcess;
                            proc->setProgram(R"(cmd.exe)");
                            QStringList list;
                            list<<R"(C:\Users\tom\Desktop\qprocess Args\qprocessZArgs.exe)"<<R"(C:\Users\tom\Desktop\folder name\that.txt)"<<R"(It works!)";
                        
                            proc->setArguments(QStringList()<<"/C"<<list);
                            proc->start();
                        

                        But it doesn't work ( no create file ).

                        S Offline
                        S Offline
                        SimonSchroeder
                        wrote on last edited by
                        #15

                        @TomNow99 said in How to use QProcess with setArguments() and folders with double word name?:

                        proc = new QProcess;
                        proc->setProgram(R"(cmd.exe)");
                        QStringList list;
                        list<<R"(C:\Users\tom\Desktop\qprocess Args\qprocessZArgs.exe)"<<R"(C:\Users\tom\Desktop\folder name\that.txt)"<<R"(It works!)";
                        
                        proc->setArguments(QStringList()<<"/C"<<list);
                        proc->start();
                        

                        This approach most likely does not work as you forgot to provide /c to cmd.exe which tells it to interpret the rest of the arguments as program to run. If you do not have any reason to use cmd, I would suggest the following instead:

                        proc = new QProcess;
                        proc->setProgram(R"(C:\Users\tom\Desktop\qprocess Args\qprocessZArgs.exe)");
                        QStringList list;
                        list<<R"(C:\Users\tom\Desktop\folder name\that.txt)"<<R"(It works!)";
                        
                        proc->setArguments(QStringList()<<"/C"<<list);
                        proc->start();
                        

                        If you still want to go with cmd I am not entirely sure how things after /c are handled. It could be that everything needs to be one single string which I don't actually know. In that case you would have to have a QStringList with only two entries: /c as the first entry and your program with all its parameters as the second. You need to figure out what cmd expects.

                        JonBJ 1 Reply Last reply
                        0
                        • S SimonSchroeder

                          @TomNow99 said in How to use QProcess with setArguments() and folders with double word name?:

                          proc = new QProcess;
                          proc->setProgram(R"(cmd.exe)");
                          QStringList list;
                          list<<R"(C:\Users\tom\Desktop\qprocess Args\qprocessZArgs.exe)"<<R"(C:\Users\tom\Desktop\folder name\that.txt)"<<R"(It works!)";
                          
                          proc->setArguments(QStringList()<<"/C"<<list);
                          proc->start();
                          

                          This approach most likely does not work as you forgot to provide /c to cmd.exe which tells it to interpret the rest of the arguments as program to run. If you do not have any reason to use cmd, I would suggest the following instead:

                          proc = new QProcess;
                          proc->setProgram(R"(C:\Users\tom\Desktop\qprocess Args\qprocessZArgs.exe)");
                          QStringList list;
                          list<<R"(C:\Users\tom\Desktop\folder name\that.txt)"<<R"(It works!)";
                          
                          proc->setArguments(QStringList()<<"/C"<<list);
                          proc->start();
                          

                          If you still want to go with cmd I am not entirely sure how things after /c are handled. It could be that everything needs to be one single string which I don't actually know. In that case you would have to have a QStringList with only two entries: /c as the first entry and your program with all its parameters as the second. You need to figure out what cmd expects.

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

                          @SimonSchroeder said in How to use QProcess with setArguments() and folders with double word name?:

                          This approach most likely does not work as you forgot to provide /c to cmd.exe

                          Huh? Yes, he does, you quoted his:

                          proc->setArguments(QStringList()<<"/C"<<list);
                          

                          It could be that everything needs to be one single string which I don't actually know.

                          My understanding is that, unlike Linux /bin/{sh,bash} -c, cmd /c is actually followed by a line of tokens, not a single quoted argument.

                          You need to figure out what cmd expects.

                          That's why I told him to read carefully through the output of cmd /?, which goes through in detail the handling of /c and quoting.

                          1 Reply Last reply
                          1
                          • S Offline
                            S Offline
                            SimonSchroeder
                            wrote on last edited by
                            #17

                            @JonB said in How to use QProcess with setArguments() and folders with double word name?:

                            Huh? Yes, he does, you quoted his:
                            proc->setArguments(QStringList()<<"/C"<<list);

                            Yes, you are right. I read over it too quickly.

                            1 Reply Last reply
                            1

                            • Login

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