QProccess stdout displayed only on first run in QTextEdit
-
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?
-
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?
@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 yourreadAll()
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. CallingreadAll()
"clears" thereadyReadStandardOutput
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 timeprocStart()
is called you will do yourconnect()
s again. You will then end up with multiple calls toreadstout()
. The last one called will blatself.stdOut
from multiple calls toreadAll()
, which may well be blank. Put in aprint()
debug statement to see the slot being called. -
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.
-
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.
@dancaer69 Actually you could simply do
def readstout(self): self.stdOut.append(str(self.process.readAll(),encoding="utf8"))