I have a main pyqt program that needs to run external program with arguments. I would like to use a QDialog as a sort of a status monitor that would capture the external program's stdout while it is executing and display them in a textbox inside the QDialog. I have the following status monitor code:
class ProgressInfo(QtGui.QDialog):
def __init__(self, cmd, args, parent=None):
#super(self).__init__(parent)
QDialog.__init__(self)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.cmd = cmd
self.args = args
self.keepGoing = True
layout = QFormLayout()
layout.setContentsMargins(10, 10, 10, 10)
self.output = QtGui.QTextEdit()
layout.addRow(self.output)
layout.addRow(self.ui.buttonBox)
self.setLayout(layout)
self.ext_process = QtCore.QProcess(self)
#self.ext_process.waitForFinished(-1)
#self.ext_process.waitForStarted()
#self.ext_process.readyRead.connect(self.dataReady)
self.ext_process.started.connect(self.open)
self.ext_process.readyReadStandardOutput.connect(self.dataReady)
self.ext_process.finished.connect(self.onProcessFinished)
self.ext_process.start(self.cmd, self.args)
def dataReady(self):
cursor = self.output.textCursor()
cursor.movePosition(cursor.End)
cursor.insertText(str(self.ext_process.readAll()))
self.output.ensureCursorVisible()
def onProcessFinished(self):
cursor = self.output.textCursor()
cursor.movePosition(cursor.End)
#cursor.insertText(str(self.ext_process.readAll()))
cursor.insertText(str(self.ext_process.readAllStandardOutput()))
self.output.ensureCursorVisible()
Then I would instantiate this with the following command:
prog='C:/Program Files (x86)/My Program/Execute.exe'
margs=['D:/Data/Input1.txt', 'D:/Data/Input2.txt']
status = ProgressInfo(prog, margs, self)
So far this hasn't worked yet.
Problem 1: the external program will run only after I uncommented out the waitForFinished(-1) line.
Problem 2. the QDialog box only open in a flash, then disappears.
Problem 3. obviously, no standout from the running program is displayed.
Lastly, the code I put together draws on many people's ideas and lessons, but I look at it, it seems that it can only print out all of the standout after the program finished, but I was hoping it will display line by line as the program is writing them out at runtime.
My tool chains: Python 64-bit version 2.7.5 and I am developing on Windows 7 box
('Qt version:', '4.8.5')
('SIP version:', '4.14.7')
('PyQt version:', '4.10.2')
Thanks for any help.