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
Qt 6.11 is out! See what's new in the release blog

The missing startDetached overloaded function

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 6 Posters 10.8k 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
    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