0

i am experiencing a quite strange bug. I am trying to redirect the QProcess output to a textbrowser.append() in debugging mode it works in VS and VSCode but it does not work in release mode. The signals readyReadStandardError and readyReadStandardOut do not emit a signal during the process. Only when it has finished. But i need it to emit a signal during the process.

i have tried everything like

  • seeking for problems in the channels (setProcessChannelMode(QtCore.QProcess.ForwardedOutputChannel) is working and printing in realtime into main.sys.stdout but i need only the stdout from the Qprocess to transfer to QtextBrowser. When i switch the Channel to SeparateChannels or MergedChannels i get no output.
  • Tried different signal methods (-started(), -errorOccured())

my Code is looking like that:

    self.process = QtCore.QProcess()
    self.process.setProgram("python")
    self.process.setArguments(["run.py"])

    #both not working when no debugging mode in VSCode
    #self.process.readyReadStandardError.connect(self.on_readyReadStandardError)
    #self.process.readyReadStandardOutput.connect(self.on_readyReadStandardOutput)

    #ForwardedOutputChannel is working
    self.process.setProcessChannelMode(QtCore.QProcess.ForwardedOutputChannel)

    #another try not working
    #self.process.setReadChannel(QtCore.QProcess.StandardOutput)

    #tried here a different signal with no luck
    #self.process.started.connect(self.on_readyReadStandardOutput)
    
    self.pushButton.clicked.connect(self.accepted)

  #starting process on button click
  def accepted(self):
    self.process.start()

  #what to do when readyReadStandardOutput signal is emitted - i did the same for stderr
  def on_readyReadStandardOutput(self):
    print("hi") #test if the signal is being send
    out = self.processSubscribe.readAllStandardOutput().data().decode().strip()
    self.textBrowser.append(out)

EDIT here you have a small snippet for testing:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
        self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
        self.textBrowser.setObjectName("textBrowser")
        self.gridLayout.addWidget(self.textBrowser, 1, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))

        self.process = QtCore.QProcess()
        self.process.setProgram("python")
        self.process.setArguments(["[INPUT PATH TO FILE]\\run.py"])

        #both not working when no debugging mode in VSCode
        self.process.readyReadStandardError.connect(self.on_readyReadStandardError)
        self.process.readyReadStandardOutput.connect(self.on_readyReadStandardOutput)

        #ForwardedOutputChannel is working
        #self.process.setProcessChannelMode(QtCore.QProcess.ForwardedOutputChannel)

        self.pushButton.clicked.connect(self.accepted)

    #starting process on button click
    def accepted(self):
        self.process.start()

  #what to do when readyReadStandardOutput signal is emitted - i did the same for stderr
    def on_readyReadStandardOutput(self):
        print("hi") #test if the signal is being send
        out = self.process.readAllStandardOutput().data().decode().strip()
        self.textBrowser.append(out)

    def on_readyReadStandardError(self):
        err = self.process.readAllStandardError().data().decode().strip()
        self.textBrowser.append(err)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

the run.py looks like this (external python programm for QProcess)


import time
for i in range(5):
    print(i)
    time.sleep(1)

just copy and try yourself

hamze55_
  • 1
  • 4

1 Answers1

0

I have found an answer right here. I have editet run.py with the line

sys.stdout.flush()

so:

import time
for i in range(5):
  print(i)
  sys.stdout.flush()
  time.sleep(1)
hamze55_
  • 1
  • 4