Reputation: 27
I have an application that uses a lot of QProcess and sometimes two processes run at the same time. That's why I have a lot of functions for readyReadStandardOutput.connect(). so my idea is to create two or three classes for similar situations and reduce code a lot. I have been able to check that in this question, there is a solution that I like
https://stackoverflow.com/questions/41728959....
and there is another one:
https://stackoverflow.com/questions/41728959...
I have tried to create a class that can help me from the class of the first link, however I have not been successful, because it generates error
QObject::connect: Cannot connect ProcessOutputReader::produce_output(QString) to (null)::append_output(QString)
Traceback (most recent call last):
File "process2.py", line 22, in funRun
self.process.run_process()
File "process2.py", line 40, in run_process
self.process.produce_output.connect(self.append_output)
TypeError: connect() failed between ProcessOutputReader.produce_output[str] and append_output()
this is my code:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class windowProcess(QMainWindow):
def __init__(self):
super(windowProcess, self).__init__()
self.setWindowTitle("Windows Process")
self.resize(475, 253)
self.btn_run = QPushButton("run",self)
self.btn_run.setGeometry(388, 20, 75, 25)
self.text_edit = QTextEdit(self)
self.text_edit.setGeometry(20, 50, 441, 191)
self.btn_run.clicked.connect(self.funRun)
def funRun(self):
self.process = myClassProcess()
self.process.run_process()
@pyqtSlot(str)
def append_output(self, text):
self.text_edit.append(text)
@pyqtSlot(str)
def append_error(self, text):
self.text_edit.append(text)
class myClassProcess(object):
def __init__(self, parent=None):
self.info = []
def run_process(self):
self.process = ProcessOutputReader()
cmd = "ping www.google.com"
self.process.produce_output.connect(self.append_output)
self.process.produce_error.connect(self.append_error)
self.process.start(cmd)
@pyqtSlot(str)
def append_output(self, text):
print(text)
self.info.append(text)
#return text
@pyqtSlot(str)
def append_error(self, text):
self.info.append(text)
#return text
def getInfo(self):
print(self.info)
class ProcessOutputReader(QProcess):
produce_output = pyqtSignal(str)
produce_error = pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent=parent)
codec = QTextCodec.codecForLocale()
self._decoder_stdout = codec.makeDecoder()
# only necessary when stderr channel isn't merged into stdout:
self._decoder_stderr = codec.makeDecoder()
self.readyReadStandardOutput.connect(self._ready_read_standard_output)
# only necessary when stderr channel isn't merged into stdout:
self.readyReadStandardError.connect(self._ready_read_standard_error)
@pyqtSlot()
def _ready_read_standard_output(self):
raw_bytes = self.readAllStandardOutput()
text = self._decoder_stdout.toUnicode(raw_bytes)
self.produce_output.emit(text)
# only necessary when stderr channel isn't merged into stdout:
@pyqtSlot()
def _ready_read_standard_error(self):
raw_bytes = self.readAllStandardError()
text = self._decoder_stderr.toUnicode(raw_bytes)
self.produce_error.emit(text)
if __name__ == '__main__':
app = QApplication(sys.argv)
ventana = windowProcess()
ventana.show()
sys.exit(app.exec_())
Upvotes: 1
Views: 281
Reputation: 244301
The pyqtSlot decorator only has meaning for a QObject, in my case myClassProcess is not causing the problem you are pointing out.
Given this there are 2 solutions:
Do not use @pyqtSlot
in append_output
and append_error
methods of myClassProcess.
Or make myClassProcess a QObject:
class myClassProcess(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self.info = []
Upvotes: 1