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 Update on Monday, May 27th 2025

How to redirect stdout/etc of detached QProcess

Scheduled Pinned Locked Moved Solved General and Desktop
qprocess
11 Posts 4 Posters 2.4k Views
  • 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.
  • Kent-DorfmanK Offline
    Kent-DorfmanK Offline
    Kent-Dorfman
    wrote on last edited by
    #2

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

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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

        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!

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #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 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.

          JonBJ 1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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

              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.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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
              2
              • JonBJ JonB

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

                JonBJ 1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 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

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #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
                    2
                    • JonBJ JonB

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

                      • Login

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