Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QProccess stdout displayed only on first run in QTextEdit

QProccess stdout displayed only on first run in QTextEdit

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 3 Posters 610 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.
  • D Offline
    D Offline
    dancaer69
    wrote on last edited by dancaer69
    #1

    I was trying to run an os command(linux), and after I tried some different ways I ended up to use QProcess. With QProcess I' ve manage to do what I wanted, but only the first time I run the process.
    I have a QTextEdit in which the output of process displayed and then I hide it with a button. This works fine the first time the process running and displaying the output. But If I run it another time, then QTextEdit is empty. I'm sure that the process running fine, just the output doesn't show up.

    This is the method I'm using to run the process:

        def procStart(self, cmd):
            self.process.setProcessChannelMode(QProcess.MergedChannels)
            self.process.readyReadStandardOutput.connect(self.readstout)
            self.process.finished.connect(self.dlFinished)
            self.process.start(cmd)
    

    And here are the other two connect methods:

        def readstout(self):
            self.stdOut.show()
            self.bDismiss.show()
            self.bDismiss.setEnabled(False)
            self.stdOut.setText(str(self.process.readAll(),encoding="utf8"))
    
        def dlFinished(self):
            if self.process.state()==self.process.NotRunning:
                if not self.stdOut.toPlainText().startswith("ERROR:"):
                    self.msgDlg.dialogTypes("DL_FINISH")
                    print("Finish")
                else:
                    self.msgDlg.dialogTypes("DL_ERROR")
                    print("Error")
            self.process.close()
    

    Do I need to do something else with qprocess?

    JonBJ 1 Reply Last reply
    0
    • D dancaer69

      I was trying to run an os command(linux), and after I tried some different ways I ended up to use QProcess. With QProcess I' ve manage to do what I wanted, but only the first time I run the process.
      I have a QTextEdit in which the output of process displayed and then I hide it with a button. This works fine the first time the process running and displaying the output. But If I run it another time, then QTextEdit is empty. I'm sure that the process running fine, just the output doesn't show up.

      This is the method I'm using to run the process:

          def procStart(self, cmd):
              self.process.setProcessChannelMode(QProcess.MergedChannels)
              self.process.readyReadStandardOutput.connect(self.readstout)
              self.process.finished.connect(self.dlFinished)
              self.process.start(cmd)
      

      And here are the other two connect methods:

          def readstout(self):
              self.stdOut.show()
              self.bDismiss.show()
              self.bDismiss.setEnabled(False)
              self.stdOut.setText(str(self.process.readAll(),encoding="utf8"))
      
          def dlFinished(self):
              if self.process.state()==self.process.NotRunning:
                  if not self.stdOut.toPlainText().startswith("ERROR:"):
                      self.msgDlg.dialogTypes("DL_FINISH")
                      print("Finish")
                  else:
                      self.msgDlg.dialogTypes("DL_ERROR")
                      print("Error")
              self.process.close()
      

      Do I need to do something else with qprocess?

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

      @dancaer69 said in QProccess stdout displayed only on first run in QTextEdit:

      def readstout(self):
          self.stdOut.setText(str(self.process.readAll(),encoding="utf8"))
      

      I assume your problem arises from what is wrong is here. readyReadStandardOutput can fire multiple times, you cannot guarantee that calling your readAll() will read all the output ever produced by the process. It will merely read what happens to be there at the instant it is called, which could be anywhere from 1 byte to the totality of the (remaining) output. Calling readAll() "clears" the readyReadStandardOutput signal state, so it will be emitted again on any further output arriving.

      You must buffer up/append all the output you get this way until finished signal received.

      When you have coded this correctly, does that somehow make your problem go away?

      Alternatively: You seem to be re-using the self.process variable. Each time procStart() is called you will do your connect()s again. You will then end up with multiple calls to readstout(). The last one called will blat self.stdOut from multiple calls to readAll(), which may well be blank. Put in a print() debug statement to see the slot being called.

      1 Reply Last reply
      2
      • D Offline
        D Offline
        dancaer69
        wrote on last edited by dancaer69
        #3

        Thanks. I'm programming as a hobby from time to time for personal use, so I'm a noob and was difficult to understand what to do. But you helped me to understand the problem and after I searched a bit I found some relative stuff.
        So, I changed the readstout() method like this:

            def readstout(self):
                self.stdOut.show()
                self.bDismiss.show()
                self.bDismiss.setEnabled(False)
                while (self.process.canReadLine()):
                    self.stdOut.setText(str(self.process.readLine(),encoding="utf8"))
        

        and now the output appears every time I start the process.

        jsulmJ 1 Reply Last reply
        0
        • D dancaer69

          Thanks. I'm programming as a hobby from time to time for personal use, so I'm a noob and was difficult to understand what to do. But you helped me to understand the problem and after I searched a bit I found some relative stuff.
          So, I changed the readstout() method like this:

              def readstout(self):
                  self.stdOut.show()
                  self.bDismiss.show()
                  self.bDismiss.setEnabled(False)
                  while (self.process.canReadLine()):
                      self.stdOut.setText(str(self.process.readLine(),encoding="utf8"))
          

          and now the output appears every time I start the process.

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

          @dancaer69 Actually you could simply do

          def readstout(self):
              self.stdOut.append(str(self.process.readAll(),encoding="utf8"))
          

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

          1 Reply Last reply
          3

          • Login

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