views:

227

answers:

3

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.

+1  A: 

I 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.

continued...

Nick T
Great help Bro, it really have something for me.but wat i got is about windows only.i need to make system calls using it.ie its about executing some programs with req arguments.but it was great help bro as i could get the way to implement the application interface. Thanks
Mr.K
again tried and got it successfully done bro. I used start() unstead of closeStdin() it workd smoothly.thanks
Mr.K
A: 

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
AndiDog
the idea is good bro, but the problem is i dot kno if the thread is able to transfer what is on out out till now. because i dont think we have enuf time diffrence for updation. But i could find the answer from above answer by NIck. Thanks for helping me andiDog.
Mr.K
+1  A: 
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.

Mr.K