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. The missing startDetached overloaded function
Forum Updated to NodeBB v4.3 + New Features

The missing startDetached overloaded function

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 6 Posters 9.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.
  • jsulmJ jsulm

    @Aman-Jiang No, arguments list can be empty, QProcess doesn't care whether the executable you want to start expects any parameters or not. You should check why it didn't work.

    A Offline
    A Offline
    Aman Jiang
    wrote on last edited by
    #5

    @jsulm Are you sure ?

    On Windows 10, Visual Studio 2013, Qt5.5.1, this code will open notepad:

    QProcess::startDetached("notepad.exe", QStringList(), R"(C:\Users\Aman\Desktop)");
    

    And this, nothing happened:

    QProcess::startDetached("notepad.exe a.txt", QStringList(), R"(C:\Users\Aman\Desktop)");
    

    There is exactly a txt file called 'a.txt' exists in 'C:\Users\Aman\Desktop' directory but this is not the point, the point is that notepad is not started, you know notepad can starts with error arguments.

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

      Because your calling it wrong. a.text is an argument so put it in the argument list.

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

      A 2 Replies Last reply
      0
      • A Aman Jiang

        @jsulm Are you sure ?

        On Windows 10, Visual Studio 2013, Qt5.5.1, this code will open notepad:

        QProcess::startDetached("notepad.exe", QStringList(), R"(C:\Users\Aman\Desktop)");
        

        And this, nothing happened:

        QProcess::startDetached("notepad.exe a.txt", QStringList(), R"(C:\Users\Aman\Desktop)");
        

        There is exactly a txt file called 'a.txt' exists in 'C:\Users\Aman\Desktop' directory but this is not the point, the point is that notepad is not started, you know notepad can starts with error arguments.

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

        @Aman-Jiang Yes, I'm sure. What I mean is: if you don't have any parameters then the list can be empty. Your second example is wrong: if you have any parameters then put them in the list:

        QProcess::startDetached("notepad.exe", QStringList() << "a.txt", R"(C:\Users\Aman\Desktop)");
        

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

        A 1 Reply Last reply
        0
        • SGaistS SGaist

          Because your calling it wrong. a.text is an argument so put it in the argument list.

          A Offline
          A Offline
          Aman Jiang
          wrote on last edited by
          #8

          @SGaist I know that.

          This code will works fine:

          QProcess::startDetached("notepad.exe a.txt");
          

          But there is no 'workdir' argument. This is what I am talking about. I need this version but it's missing:

          bool startDetached(const QString & program, const QString & workingDirectory, qint64 * pid = 0);
          
          jsulmJ 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Aman-Jiang Yes, I'm sure. What I mean is: if you don't have any parameters then the list can be empty. Your second example is wrong: if you have any parameters then put them in the list:

            QProcess::startDetached("notepad.exe", QStringList() << "a.txt", R"(C:\Users\Aman\Desktop)");
            
            A Offline
            A Offline
            Aman Jiang
            wrote on last edited by
            #9

            @jsulm I know it's wrong, and I know how to call this function. But I don't need this version, I need this one:

            bool startDetached(const QString & program, const QString & workingDirectory, qint64 * pid = 0);
            

            The program and arguments in one string, and a working dir argument.

            I am talking about overloaded functions.

            1 Reply Last reply
            0
            • A Aman Jiang

              @SGaist I know that.

              This code will works fine:

              QProcess::startDetached("notepad.exe a.txt");
              

              But there is no 'workdir' argument. This is what I am talking about. I need this version but it's missing:

              bool startDetached(const QString & program, const QString & workingDirectory, qint64 * pid = 0);
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by jsulm
              #10

              @Aman-Jiang
              Why not use this one with empty parameter list?

              bool startDetached(const QString & program, const QStringList & arguments, const QString & workingDirectory, qint64 * pid = 0);
              

              like

              startDetached("notepad.exe", QStringList(), "c:\\temp", 123);
              

              I don't see any need for one more method.

              "The program and arguments in one string" - don't do that! Arguments should be passed as arguments list.

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

              A 1 Reply Last reply
              1
              • SGaistS SGaist

                Because your calling it wrong. a.text is an argument so put it in the argument list.

                A Offline
                A Offline
                Aman Jiang
                wrote on last edited by
                #11

                I am working on some kind of launcher. There is many command lines from the user, these command lines is used to launch the specified programs, there're stored in db. I don't want to split these command lines into QStringList on launching, because the target programs could be very different, it's risk, I want to use them directly.

                jsulmJ 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @Aman-Jiang
                  Why not use this one with empty parameter list?

                  bool startDetached(const QString & program, const QStringList & arguments, const QString & workingDirectory, qint64 * pid = 0);
                  

                  like

                  startDetached("notepad.exe", QStringList(), "c:\\temp", 123);
                  

                  I don't see any need for one more method.

                  "The program and arguments in one string" - don't do that! Arguments should be passed as arguments list.

                  A Offline
                  A Offline
                  Aman Jiang
                  wrote on last edited by
                  #12

                  @jsulm This version accepts program and arguments in one string:

                  bool startDetached(const QString & program);
                  

                  You couldn't always pass arguments as arguments list, because the split rule can be different, a program may like semicolon more than space.

                  On windows, GetCommandLine returns just one string.

                  kshegunovK 1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    @Aman-Jiang said:

                    startDetached

                    While it can work with params with the exe string
                    , it will break very easy

                    QProcess::startDetached("notepad.exe E:/space in path/test.txt");

                    wont work.

                    There is nothing stopping you from doing
                    startDetached("notepad.exe TEST.txt", QStringList(), "c:\temp");
                    to specify work dir.
                    and "misuse" the exe string with params.

                    A 1 Reply Last reply
                    1
                    • A Aman Jiang

                      I am working on some kind of launcher. There is many command lines from the user, these command lines is used to launch the specified programs, there're stored in db. I don't want to split these command lines into QStringList on launching, because the target programs could be very different, it's risk, I want to use them directly.

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

                      @Aman-Jiang Are you aware that startDetach(const QString& program) splits the parameter you pass?
                      Read the documentation:

                      After the command string has been split and unquoted, this function behaves like the overload which takes the arguments as a string list.
                      

                      http://doc.qt.io/qt-5/qprocess.html#startDetached-1

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

                      A 1 Reply Last reply
                      0
                      • A Aman Jiang

                        @jsulm This version accepts program and arguments in one string:

                        bool startDetached(const QString & program);
                        

                        You couldn't always pass arguments as arguments list, because the split rule can be different, a program may like semicolon more than space.

                        On windows, GetCommandLine returns just one string.

                        kshegunovK Offline
                        kshegunovK Offline
                        kshegunov
                        Moderators
                        wrote on last edited by
                        #15

                        @Aman-Jiang

                        You couldn't always pass arguments as arguments list, because the split rule can be different, a program may like semicolon more than space.

                        You can and you should, and the split rule can't be different, so listen to the fellows that commented above and pass the arguments as a string list. You may need to do some parsing yourself to handle quoted/escaped strings, for which you can use something like boost's Spirit module. But as @mrjj noted, passing the arguments along with the executable in one string is just abuse of the API and may or may not work depending on OS/Qt version and other factors beyond your control.

                        Kind regards.

                        Read and abide by the Qt Code of Conduct

                        A 1 Reply Last reply
                        1
                        • mrjjM mrjj

                          @Aman-Jiang said:

                          startDetached

                          While it can work with params with the exe string
                          , it will break very easy

                          QProcess::startDetached("notepad.exe E:/space in path/test.txt");

                          wont work.

                          There is nothing stopping you from doing
                          startDetached("notepad.exe TEST.txt", QStringList(), "c:\temp");
                          to specify work dir.
                          and "misuse" the exe string with params.

                          A Offline
                          A Offline
                          Aman Jiang
                          wrote on last edited by
                          #16

                          @mrjj I don't get you. There is spaces in the string, without quotation marks, it's invalid command line, what do you want to prove ?

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

                            That it's the kind of use case you are likely going to have to fight against since you want to pass everything in a single string.

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

                            A 1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @Aman-Jiang Are you aware that startDetach(const QString& program) splits the parameter you pass?
                              Read the documentation:

                              After the command string has been split and unquoted, this function behaves like the overload which takes the arguments as a string list.
                              

                              http://doc.qt.io/qt-5/qprocess.html#startDetached-1

                              A Offline
                              A Offline
                              Aman Jiang
                              wrote on last edited by
                              #18

                              @jsulm No... I haven't notice this line in the document...

                              1 Reply Last reply
                              0
                              • kshegunovK kshegunov

                                @Aman-Jiang

                                You couldn't always pass arguments as arguments list, because the split rule can be different, a program may like semicolon more than space.

                                You can and you should, and the split rule can't be different, so listen to the fellows that commented above and pass the arguments as a string list. You may need to do some parsing yourself to handle quoted/escaped strings, for which you can use something like boost's Spirit module. But as @mrjj noted, passing the arguments along with the executable in one string is just abuse of the API and may or may not work depending on OS/Qt version and other factors beyond your control.

                                Kind regards.

                                A Offline
                                A Offline
                                Aman Jiang
                                wrote on last edited by
                                #19

                                @kshegunov Okay, I get you, I know the reason now. But, If Qt's goal is make everything easier, why not add one more overloaded startDetached ? If this one:

                                bool startDetached(const QString & program);
                                

                                Could do the right thing cross Qt versions and OS versions, add another one:

                                bool startDetached(const QString & program, const QString& workDir);
                                

                                Is more complete. At least I think so.

                                raven-worxR kshegunovK 2 Replies Last reply
                                0
                                • SGaistS SGaist

                                  That it's the kind of use case you are likely going to have to fight against since you want to pass everything in a single string.

                                  A Offline
                                  A Offline
                                  Aman Jiang
                                  wrote on last edited by
                                  #20

                                  @SGaist I used to write lots of string parsing codes, it's not very hard, but I like a more easier way to do the same work if I could.

                                  1 Reply Last reply
                                  0
                                  • A Aman Jiang

                                    @kshegunov Okay, I get you, I know the reason now. But, If Qt's goal is make everything easier, why not add one more overloaded startDetached ? If this one:

                                    bool startDetached(const QString & program);
                                    

                                    Could do the right thing cross Qt versions and OS versions, add another one:

                                    bool startDetached(const QString & program, const QString& workDir);
                                    

                                    Is more complete. At least I think so.

                                    raven-worxR Offline
                                    raven-worxR Offline
                                    raven-worx
                                    Moderators
                                    wrote on last edited by
                                    #21

                                    @Aman-Jiang
                                    there is already QProcess::setWorkingDirectory() available

                                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                    If you have a question please use the forum so others can benefit from the solution in the future

                                    A 1 Reply Last reply
                                    0
                                    • raven-worxR raven-worx

                                      @Aman-Jiang
                                      there is already QProcess::setWorkingDirectory() available

                                      A Offline
                                      A Offline
                                      Aman Jiang
                                      wrote on last edited by
                                      #22

                                      @raven-worx A QProcess object will wait for the process, a "detach" member function is missing.

                                      1 Reply Last reply
                                      0
                                      • A Aman Jiang

                                        @kshegunov Okay, I get you, I know the reason now. But, If Qt's goal is make everything easier, why not add one more overloaded startDetached ? If this one:

                                        bool startDetached(const QString & program);
                                        

                                        Could do the right thing cross Qt versions and OS versions, add another one:

                                        bool startDetached(const QString & program, const QString& workDir);
                                        

                                        Is more complete. At least I think so.

                                        kshegunovK Offline
                                        kshegunovK Offline
                                        kshegunov
                                        Moderators
                                        wrote on last edited by kshegunov
                                        #23

                                        @Aman-Jiang

                                        But, If Qt's goal is make everything easier, why not add one more overloaded startDetached ?

                                        Well, you have to ask Qt devs that, but if I were to hazard a guess - probably simplicity of implementation and compliance with QProcess::start, but I haven't checked the source so I'm just speculating. While the split rules are the same across OSes some (notably Windows) have somewhat stringent requirements for characters in paths (or rather the command line interpreter cmd does), Linux is much more relaxed on this matter.
                                        My advice is to just wrap a very simple LL(1) parser (it's so simple you can even write it by hand in an hour) and use it to split your command line.

                                        "detach" member function is missing.

                                        I'm not sure you can detach from a process freely on all platforms, so I'm assuming it's not missing, it's just impossible to provide in a cross-platform consistent manner.

                                        Kind regards.

                                        Read and abide by the Qt Code of Conduct

                                        A 1 Reply Last reply
                                        0
                                        • kshegunovK kshegunov

                                          @Aman-Jiang

                                          But, If Qt's goal is make everything easier, why not add one more overloaded startDetached ?

                                          Well, you have to ask Qt devs that, but if I were to hazard a guess - probably simplicity of implementation and compliance with QProcess::start, but I haven't checked the source so I'm just speculating. While the split rules are the same across OSes some (notably Windows) have somewhat stringent requirements for characters in paths (or rather the command line interpreter cmd does), Linux is much more relaxed on this matter.
                                          My advice is to just wrap a very simple LL(1) parser (it's so simple you can even write it by hand in an hour) and use it to split your command line.

                                          "detach" member function is missing.

                                          I'm not sure you can detach from a process freely on all platforms, so I'm assuming it's not missing, it's just impossible to provide in a cross-platform consistent manner.

                                          Kind regards.

                                          A Offline
                                          A Offline
                                          Aman Jiang
                                          wrote on last edited by
                                          #24

                                          @kshegunov Okay. Thank you for your advice, I will think about that.

                                          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