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 redirect stdout/etc of detached QProcess
Forum Updated to NodeBB v4.3 + New Features

How to redirect stdout/etc of detached QProcess

Scheduled Pinned Locked Moved Solved General and Desktop
qprocess
11 Posts 4 Posters 2.5k Views 1 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.
  • K Offline
    K Offline
    Kent-Dorfman
    wrote on 8 Mar 2021, 04:28 last edited by
    #2

    out of curiosity, do you connect the file descriptor signals BEFORE starting the process?

    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 8 Mar 2021, 05:52 last edited by
      #3

      @Dariusz said in How to Re-dirrect stdout/etc of Detached QProcess:

      How can I properly redirect this?

      Don't start the process as detached...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • D Dariusz
        8 Mar 2021, 00:54

        Hey

        I think I've tried everything but still no luck.

        I start my app as

        QProcess *s = new QProcess()
        s->setProgram("Path")
        s->setArguments({"-arg=lala"}
        s->setProcessChannelMode(QProcess::ForwardedChannels);
        s->startDetached()
        

        and I connect signals to >

        readyReadStandardError
        readyReadStandardOutput
        

        But no matter which setProcessChannelMode I pick, everything always goes in to my main application std out and not my functions/signal/slots. How can I properly redirect this?

        TIA!

        J Offline
        J Offline
        JonB
        wrote on 8 Mar 2021, 08:37 last edited by JonB 3 Aug 2021, 08:44
        #4

        @Dariusz said in How to Re-dirrect stdout/etc of Detached QProcess:

        s->startDetached()

        You are choosing to use bool QProcess::startDetached(qint64 *pid = nullptr). As a result s->setProcessChannelMode(QProcess::ForwardedChannels); has no effect, and the signals will not be raised. The documentation page covers this.

        By far the easiest thing you can do is not use startDetached(), as @Christian-Ehrlicher says, and use start() instead. Why do you need to use startDetached()? "Detached" means detached from the calling process so that it is not the child process's parent, and hence cannot access stiff like child's stdout/stderr, yet that seems to be exactly what you do want to access....

        1 Reply Last reply
        1
        • D Offline
          D Offline
          Dariusz
          wrote on 8 Mar 2021, 09:03 last edited by
          #5

          Hey

          Yeh found it few hours latter. I ended up spawning QThread and running the process in that thread & spawning as ->start() instead. The startDetaching make sense, kinda wish id had more "backend" logic to handle streams/etc. I cant even disable stdout stream using the detached mode.

          J 1 Reply Last reply 8 Mar 2021, 09:20
          0
          • C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 8 Mar 2021, 09:06 last edited by
            #6

            @Dariusz said in How to Re-dirrect stdout/etc of Detached QProcess:

            I cant even disable stdout stream using the detached mode.

            That's why it's called 'detached' ...

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • D Dariusz
              8 Mar 2021, 09:03

              Hey

              Yeh found it few hours latter. I ended up spawning QThread and running the process in that thread & spawning as ->start() instead. The startDetaching make sense, kinda wish id had more "backend" logic to handle streams/etc. I cant even disable stdout stream using the detached mode.

              J Offline
              J Offline
              JonB
              wrote on 8 Mar 2021, 09:20 last edited by
              #7

              @Dariusz said in How to Re-dirrect stdout/etc of Detached QProcess:

              I ended up spawning QThread and running the process in that thread & spawning as ->start() instead.

              That sounds quite wrong. There is no need for any thread here, QProcess::start() and all the signals are asynchronous, what do you want any thread for??

              D 1 Reply Last reply 8 Mar 2021, 12:22
              2
              • J JonB
                8 Mar 2021, 09:20

                @Dariusz said in How to Re-dirrect stdout/etc of Detached QProcess:

                I ended up spawning QThread and running the process in that thread & spawning as ->start() instead.

                That sounds quite wrong. There is no need for any thread here, QProcess::start() and all the signals are asynchronous, what do you want any thread for??

                D Offline
                D Offline
                Dariusz
                wrote on 8 Mar 2021, 12:22 last edited by
                #8

                @JonB said in How to Re-dirrect stdout/etc of Detached QProcess:

                @Dariusz said in How to Re-dirrect stdout/etc of Detached QProcess:

                I ended up spawning QThread and running the process in that thread & spawning as ->start() instead.

                That sounds quite wrong. There is no need for any thread here, QProcess::start() and all the signals are asynchronous, what do you want any thread for??

                I don't want to block the main thread.
                I want to run the app and then manage it from my GUI while it runs.

                J 1 Reply Last reply 8 Mar 2021, 12:57
                0
                • C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 8 Mar 2021, 12:24 last edited by
                  #9

                  @Dariusz said in How to Re-dirrect stdout/etc of Detached QProcess:

                  I don't want to block the main thread.

                  QProcess is async as @JonB already said so why do you block the main thread when you run a QProcess there?

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  2
                  • D Dariusz
                    8 Mar 2021, 12:22

                    @JonB said in How to Re-dirrect stdout/etc of Detached QProcess:

                    @Dariusz said in How to Re-dirrect stdout/etc of Detached QProcess:

                    I ended up spawning QThread and running the process in that thread & spawning as ->start() instead.

                    That sounds quite wrong. There is no need for any thread here, QProcess::start() and all the signals are asynchronous, what do you want any thread for??

                    I don't want to block the main thread.
                    I want to run the app and then manage it from my GUI while it runs.

                    J Offline
                    J Offline
                    JonB
                    wrote on 8 Mar 2021, 12:57 last edited by JonB 3 Aug 2021, 12:58
                    #10

                    @Dariusz said in How to Re-dirrect stdout/etc of Detached QProcess:

                    I don't want to block the main thread.

                    And? I know that. Like I said, QProcess::start/startDetached() do not block anything. As I wrote, they are already asynchronous, and there is no need for any threading of your own.

                    D 1 Reply Last reply 8 Mar 2021, 13:45
                    2
                    • J JonB
                      8 Mar 2021, 12:57

                      @Dariusz said in How to Re-dirrect stdout/etc of Detached QProcess:

                      I don't want to block the main thread.

                      And? I know that. Like I said, QProcess::start/startDetached() do not block anything. As I wrote, they are already asynchronous, and there is no need for any threading of your own.

                      D Offline
                      D Offline
                      Dariusz
                      wrote on 8 Mar 2021, 13:45 last edited by
                      #11

                      @JonB Wait really !? I could swear that they were blocking methods hmmmmmmmmmmmmmmmmmmmmmmmmmmmmm my bad! Thanks :D

                      1 Reply Last reply
                      2

                      11/11

                      8 Mar 2021, 13:45

                      • Login

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