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 pipe qprocess' stdout to father process as stdin

how to pipe qprocess' stdout to father process as stdin

Scheduled Pinned Locked Moved Unsolved General and Desktop
37 Posts 4 Posters 16.3k Views 2 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.
  • I Offline
    I Offline
    Isolde
    wrote on last edited by
    #1

    I found a methed of QProcess named setStandardOutputProcessalt text
    But I want my QProcess pipe it stdout to father process, How can I do that?

    jsulmJ 1 Reply Last reply
    0
    • I Isolde

      I found a methed of QProcess named setStandardOutputProcessalt text
      But I want my QProcess pipe it stdout to father process, How can I do that?

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

      @Isolde Is it really needed to do this at runtime? If not then you can just start your app like this:

      my_app | some_other_app
      

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

      I 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Isolde Is it really needed to do this at runtime? If not then you can just start your app like this:

        my_app | some_other_app
        
        I Offline
        I Offline
        Isolde
        wrote on last edited by
        #3

        @jsulm Of course it works! But i think its a little bit inconvenient, so I want a runtime way to do that.

        JonBJ 1 Reply Last reply
        0
        • I Isolde

          @jsulm Of course it works! But i think its a little bit inconvenient, so I want a runtime way to do that.

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

          @Isolde

          But I want my QProcess pipe it stdout to father process, How can I do that?

          What do you mean by this, what are you trying to do?

          • If you want to pipe to a child process from your program, you create the child via QProcess and use setStandardOutputProcess, as shown in the example.

          • If by "father" process you mean the parent process to your program (e.g. the "shell" from which it is launched) then you can't --- child cannot change parent's file descriptors, it's up to the parent process to decide what to do with the child's stdout at the time it creates it, in effect the shell parent already has child's stdout to do with it as it pleases.

          jsulmJ 1 Reply Last reply
          0
          • JonBJ JonB

            @Isolde

            But I want my QProcess pipe it stdout to father process, How can I do that?

            What do you mean by this, what are you trying to do?

            • If you want to pipe to a child process from your program, you create the child via QProcess and use setStandardOutputProcess, as shown in the example.

            • If by "father" process you mean the parent process to your program (e.g. the "shell" from which it is launched) then you can't --- child cannot change parent's file descriptors, it's up to the parent process to decide what to do with the child's stdout at the time it creates it, in effect the shell parent already has child's stdout to do with it as it pleases.

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

            @JNBarchan I guess the question here is: what do you pass to setStandardOutputProcess? You would need a QProcess instance for your own process.

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

            JonBJ 1 Reply Last reply
            0
            • jsulmJ jsulm

              @JNBarchan I guess the question here is: what do you pass to setStandardOutputProcess? You would need a QProcess instance for your own process.

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

              @jsulm
              I still don't understand the question/objective, you seem to more then I do. Does "father process" mean "self process"? The way you phrase it, do you/OP mean "pipe a process's own stdout to its own stdin"?? Can you explain "clearly", then I might understand?

              jsulmJ I 3 Replies Last reply
              0
              • JonBJ JonB

                @jsulm
                I still don't understand the question/objective, you seem to more then I do. Does "father process" mean "self process"? The way you phrase it, do you/OP mean "pipe a process's own stdout to its own stdin"?? Can you explain "clearly", then I might understand?

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

                @JNBarchan Original question: "But I want my QProcess pipe it stdout to father process, How can I do that?"
                So, the Qt app shall pass its stdout to stdin from another process (if I understood the question from @Isolde correctly as the question isn't formulated clearly).
                So how would you do this:

                QProcess process2;
                process2.setStandardOutputProcess(/* What to pass here? */);
                process2.start("command2");
                

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

                JonBJ 2 Replies Last reply
                0
                • JonBJ JonB

                  @jsulm
                  I still don't understand the question/objective, you seem to more then I do. Does "father process" mean "self process"? The way you phrase it, do you/OP mean "pipe a process's own stdout to its own stdin"?? Can you explain "clearly", then I might understand?

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

                  @JNBarchan said in how to pipe qprocess' stdout to father process as stdin:

                  father process

                  I guess it is a typo and means "further process", but I may be wrong...

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

                  1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @JNBarchan Original question: "But I want my QProcess pipe it stdout to father process, How can I do that?"
                    So, the Qt app shall pass its stdout to stdin from another process (if I understood the question from @Isolde correctly as the question isn't formulated clearly).
                    So how would you do this:

                    QProcess process2;
                    process2.setStandardOutputProcess(/* What to pass here? */);
                    process2.start("command2");
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @jsulm said in how to pipe qprocess' stdout to father process as stdin:

                    @JNBarchan Original question: "But I want my QProcess pipe it stdout to father process, How can I do that?"
                    So, the Qt app shall pass its stdout to stdin from another process (if I understood the question from @Isolde correctly as the question isn't formulated clearly).
                    So how would you do this:

                    QProcess process2;
                    process2.setStandardOutputProcess(/* What to pass here? */);
                    process2.start("command2");
                    

                    OK, so, the objective is:

                    1. Start a child process.
                    2. Temporarily redirect self/parent process's stdout to child's stdin.
                    3. Produce some stdout from self/parent, to be read by child.
                    4. Wait for child to terminate, or stdout to be closed to mark end of output(?) causing child to terminate.
                    5. Un-redirect self/parent process's stdout back to whereber it was previously in self/parent.

                    Maybe #2 & #5 are not to be "temporary", the whole of parent's output is to be sent to child, there's no need to "temporarily redirect & un-redirect", I don't know?

                    I think that's it?

                    jsulmJ 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @jsulm said in how to pipe qprocess' stdout to father process as stdin:

                      @JNBarchan Original question: "But I want my QProcess pipe it stdout to father process, How can I do that?"
                      So, the Qt app shall pass its stdout to stdin from another process (if I understood the question from @Isolde correctly as the question isn't formulated clearly).
                      So how would you do this:

                      QProcess process2;
                      process2.setStandardOutputProcess(/* What to pass here? */);
                      process2.start("command2");
                      

                      OK, so, the objective is:

                      1. Start a child process.
                      2. Temporarily redirect self/parent process's stdout to child's stdin.
                      3. Produce some stdout from self/parent, to be read by child.
                      4. Wait for child to terminate, or stdout to be closed to mark end of output(?) causing child to terminate.
                      5. Un-redirect self/parent process's stdout back to whereber it was previously in self/parent.

                      Maybe #2 & #5 are not to be "temporary", the whole of parent's output is to be sent to child, there's no need to "temporarily redirect & un-redirect", I don't know?

                      I think that's it?

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

                      @JNBarchan This is my understanding. It is often hard to understand questions and problem descriptions here :-)

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

                      JonBJ 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @JNBarchan This is my understanding. It is often hard to understand questions and problem descriptions here :-)

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

                        @jsulm
                        LOL, you often do a better job at understanding posts than I do! (When I ask a question I pose it very precisely & accurately! :) )

                        OK, I will think about this, at least for Linux. (You can play more easily with pipes & redirection under Linux than under Windows.) I note the OP has not declared if he has a specific target OS....

                        1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @JNBarchan Original question: "But I want my QProcess pipe it stdout to father process, How can I do that?"
                          So, the Qt app shall pass its stdout to stdin from another process (if I understood the question from @Isolde correctly as the question isn't formulated clearly).
                          So how would you do this:

                          QProcess process2;
                          process2.setStandardOutputProcess(/* What to pass here? */);
                          process2.start("command2");
                          
                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #12

                          @jsulm
                          OK, so (to help me think), in darling Perl this is:

                          open(STDOUT, "| process2");
                          print STDOUT "stuff to STDOUT";
                          close(STDOUT);
                          
                          1 Reply Last reply
                          0
                          • JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #13

                            The first, easiest, laziest Qt solution if it is acceptable is along the lines of:

                            // pre-send all output from self/parent program to temporary file
                            QString filename;
                            QProcess process2;
                            process2.setStandardInputFile(filename);
                            process2.start("command2");
                            // delete temporary file
                            

                            This is because QProcess has setStandardInputFile() but not setStandardInputProcess() like it has setStandardOutputProcess().

                            Whether this is acceptable depends on whether parent can generate all its output before starting child.

                            Otherwise I believe (untested by me, as usual!) you can just use QProcess::write() (actually QIODevice::write()):

                            QProcess process2;
                            process2.start("command2");
                            process2.write(...);  # This appears on process2's stdin
                            process2.write(...);
                            # we need to close the write channel to process2 to signal end of input
                            process2.close();  # does this do that?
                            

                            This would need testing to see if principles work. It doesn't actually do parent's stdout to child but rather whatever parent chooses to send via explicit process2.write(...);, but is that acceptable here?

                            [ P.S. https://doc.qt.io/archives/3.3/qprocess.html#writeToStdin Looks like Qt 3.x had QProcess::writeToStdin() & QProcess::closeStdin() ! But I take it those are no longer necessary as you can just do QIODevice calls. ]

                            1 Reply Last reply
                            0
                            • JonBJ JonB

                              @jsulm
                              I still don't understand the question/objective, you seem to more then I do. Does "father process" mean "self process"? The way you phrase it, do you/OP mean "pipe a process's own stdout to its own stdin"?? Can you explain "clearly", then I might understand?

                              I Offline
                              I Offline
                              Isolde
                              wrote on last edited by
                              #14

                              @JNBarchan said in how to pipe qprocess' stdout to father process as stdin:

                              @jsulm
                              I still don't understand the question/objective, you seem to more then I do. Does "father process" mean "self process"? The way you phrase it, do you/OP mean "pipe a process's own stdout to its own stdin"?? Can you explain "clearly", then I might understand?

                              I'm so sorry for my poor description, I'm not a native English speaker so I make some mistakes in jargon. And I think that "father process" means "self process".

                              JonBJ 1 Reply Last reply
                              0
                              • I Isolde

                                @JNBarchan said in how to pipe qprocess' stdout to father process as stdin:

                                @jsulm
                                I still don't understand the question/objective, you seem to more then I do. Does "father process" mean "self process"? The way you phrase it, do you/OP mean "pipe a process's own stdout to its own stdin"?? Can you explain "clearly", then I might understand?

                                I'm so sorry for my poor description, I'm not a native English speaker so I make some mistakes in jargon. And I think that "father process" means "self process".

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

                                @Isolde
                                It's not your fault, you do your best, especially if English not your first language!

                                For processes, we talk about parent process and child process. We just don't say father!

                                My last post above offers you the (only( two ways I know to achieve what you are asking for. If you can afford to you the first "send-to-file" approach that at least will work and is too much code....!

                                I 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @Isolde
                                  It's not your fault, you do your best, especially if English not your first language!

                                  For processes, we talk about parent process and child process. We just don't say father!

                                  My last post above offers you the (only( two ways I know to achieve what you are asking for. If you can afford to you the first "send-to-file" approach that at least will work and is too much code....!

                                  I Offline
                                  I Offline
                                  Isolde
                                  wrote on last edited by
                                  #16

                                  @JNBarchan said in how to pipe qprocess' stdout to father process as stdin:

                                  @Isolde
                                  It's not your fault, you do your best, especially if English not your first language!

                                  For processes, we talk about parent process and child process. We just don't say father!

                                  My last post above offers you the (only( two ways I know to achieve what you are asking for. If you can afford to you the first "send-to-file" approach that at least will work and is too much code....!

                                  I think it's a little bit different from what I want. I want to pipe a child QProcess' stdout to parent process' stdin. This question could be that how can I pass a QProcess instance for my own process to the setStandardOutputProcess method?

                                  JonBJ 1 Reply Last reply
                                  0
                                  • I Isolde

                                    @JNBarchan said in how to pipe qprocess' stdout to father process as stdin:

                                    @Isolde
                                    It's not your fault, you do your best, especially if English not your first language!

                                    For processes, we talk about parent process and child process. We just don't say father!

                                    My last post above offers you the (only( two ways I know to achieve what you are asking for. If you can afford to you the first "send-to-file" approach that at least will work and is too much code....!

                                    I think it's a little bit different from what I want. I want to pipe a child QProcess' stdout to parent process' stdin. This question could be that how can I pass a QProcess instance for my own process to the setStandardOutputProcess method?

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

                                    @Isolde

                                    I think it's a little bit different from what I want. I want to pipe a child QProcess' stdout to parent process' stdin

                                    Darn, I wish I had understood that was what you wanted at the outset!

                                    This question could be that how can I pass a QProcess instance for my own process to the setStandardOutputProcess method?

                                    Like @jsulm wrote in an earlier post above, Qt does not offer that (AFAIK).

                                    You normally accomplish this via QProcess::readAllStandardOutput() from the parent --- or you can receive signal QProcess::readyReadStandardOutput() --- but I don't know how easy/possible to connect that to parent's stdin, one usually just uses the resulting data without that. That's what I'm doing at the moment in my own code.

                                    I 1 Reply Last reply
                                    0
                                    • JonBJ JonB

                                      @Isolde

                                      I think it's a little bit different from what I want. I want to pipe a child QProcess' stdout to parent process' stdin

                                      Darn, I wish I had understood that was what you wanted at the outset!

                                      This question could be that how can I pass a QProcess instance for my own process to the setStandardOutputProcess method?

                                      Like @jsulm wrote in an earlier post above, Qt does not offer that (AFAIK).

                                      You normally accomplish this via QProcess::readAllStandardOutput() from the parent --- or you can receive signal QProcess::readyReadStandardOutput() --- but I don't know how easy/possible to connect that to parent's stdin, one usually just uses the resulting data without that. That's what I'm doing at the moment in my own code.

                                      I Offline
                                      I Offline
                                      Isolde
                                      wrote on last edited by
                                      #18

                                      @JNBarchan said in how to pipe qprocess' stdout to father process as stdin:

                                      -- but I don't know how easy/possible to connect that to parent's stdin, one usually just uses the resulting data without that. That's what I'm doing at the moment in my own code.

                                      I know this way, but my situation looks complex, because the library(libmpv) that I used only provides very high level api that only support passing option key-value pairs, so I can only passing a filename string or "-" means stdin. And unfortunately, the data source I need is provide by another program(streamlink), it support stdout. Up to now, I'm using
                                      streamlink -O | my-qt-program
                                      to make it work. But I don't deem it a best way. So I want to start streamlink inside my program and pipe its stdout into my-qt-program's stdin. Is it possible?

                                      jsulmJ JonBJ 2 Replies Last reply
                                      0
                                      • I Isolde

                                        @JNBarchan said in how to pipe qprocess' stdout to father process as stdin:

                                        -- but I don't know how easy/possible to connect that to parent's stdin, one usually just uses the resulting data without that. That's what I'm doing at the moment in my own code.

                                        I know this way, but my situation looks complex, because the library(libmpv) that I used only provides very high level api that only support passing option key-value pairs, so I can only passing a filename string or "-" means stdin. And unfortunately, the data source I need is provide by another program(streamlink), it support stdout. Up to now, I'm using
                                        streamlink -O | my-qt-program
                                        to make it work. But I don't deem it a best way. So I want to start streamlink inside my program and pipe its stdout into my-qt-program's stdin. Is it possible?

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

                                        @Isolde If you want to start external program and read its output you can do it very easily using http://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput and http://doc.qt.io/qt-5/qprocess.html#readAllStandardOutput
                                        You do not need setStandardOutputProcess for that at all.

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

                                        I 1 Reply Last reply
                                        0
                                        • jsulmJ jsulm

                                          @Isolde If you want to start external program and read its output you can do it very easily using http://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput and http://doc.qt.io/qt-5/qprocess.html#readAllStandardOutput
                                          You do not need setStandardOutputProcess for that at all.

                                          I Offline
                                          I Offline
                                          Isolde
                                          wrote on last edited by
                                          #20

                                          @jsulm My situation is a little complex, I explained in the last reply.

                                          jsulmJ 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