Hello, I'm working on developing a GUI for the recompilation of Linux kernel. For this I need to implement 4-5 Linux commands from Python. I use Qt as GUI designer. I have successfully implemented the commands using os.system()
call. But the output is obtained at the console. The real problem is the output of command is a listing that takes almost 20-25 min continuous printing. How we can transfer this console output to a text box designed in Qt. Can any one help me to implement the setSource()
operation in Qt using source as the live console outputs.
views:
227answers:
3I mostly deal with wxPython, but is http://diotavelli.net/PyQtWiki/Capturing_Output_from_a_Process a solution that would work?
From the page:
Problem: You want to run a process that prints lots of information to the console and display the output in a text editor or browser, but the result is a GUI that freezes until the process is finished.
Solution (one of many possible): Create a QProcess object, connect its signals to some slots in your class, pass it the required arguments and start it. Data on the process's stdout and stderr is delivered to your slots.
Using a pipe comes to mind. You could use a background thread that reads the output of the program (and sends events to the GUI whenever a new line is added).
So the basic idea is this:
os.chdir("/usr/src/linux-2.6.34")
p = os.popen("make", "r")
try:
while True:
line = p.readline()
if not line:
break
# Replace this with a GUI update event (don't know anything about Qt, sorry)
print line
finally:
# Cf. http://docs.python.org/library/os.html#os.popen
programReturnValue = p.close() or 0
self.process = QProcess()
self.connect(self.process, SIGNAL("readyReadStdout()"), self.readOutput)
self.connect(self.process, SIGNAL("readyReadStderr()"), self.readErrors)
tarsourcepath="sudo tar xvpf "+ self.path1
self.process.setArguments(QStringList.split(" ",tarsourcepath))
self.process.start()
def readOutput(self):
self.textBrowser2.append(QString(self.process.readStdout()))
if self.process.isRunning()==False:
self.textBrowser2.append("\n Completed Successfully")
def readErrors(self):
self.textBrowser2.append("error: " + QString(self.process.readLineStderr()))
This did the work quite good for me. thank you all.