Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: [question]redirecting stdout to display via widget?

  1. #1
    Join Date
    Jan 2009
    Location
    Pakistan
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default [question]redirecting stdout to display via widget?

    Hi everyone.
    I'm very new to PyQt4 (its my fifth day) and I would like some help with this program. I'm getting familiar with different classes and have only started to wrap my head around the SIGNAL/SLOT concept.
    I want to redirect the output of the system call in the code to QTextBrowser, but I'm missing something basic here. Maybe use a different widget? conceptual mistake?

    Qt Code:
    1. import sys
    2. from PyQt4 import QtGui, QtCore
    3. from subprocess import Popen, PIPE
    4.  
    5. class dialerGui(QtGui.QWidget):
    6. def __init__(self, parent=None):
    7. QtGui.QWidget.__init__(self, parent)
    8.  
    9. self.setWindowTitle('PTCL')
    10. self.setWindowIcon(QtGui.QIcon('/usr/share/pixmaps/idle.xpm'))
    11.  
    12. dial = QtGui.QPushButton('Dial', self)
    13. self.connect(dial, QtCore.SIGNAL('clicked()'), self.slotDial)
    14.  
    15. quit = QtGui.QPushButton('Disconnect', self)
    16. self.connect(quit, QtCore.SIGNAL('clicked()'), QtGui.qApp, QtCore.SLOT('quit()'))
    17.  
    18. output = QtGui.QLabel('Output')
    19.  
    20. displaybox = QtGui.QTextBrowser()
    21. self.connect(dial, QtCore.SIGNAL('clicked()'), self.showOutput)
    22.  
    23. grid = QtGui.QGridLayout()
    24. grid.setSpacing(10)
    25. grid.addWidget(dial, 1, 0)
    26. grid.addWidget(quit, 1, 1)
    27. grid.addWidget(output, 2, 0)
    28. grid.addWidget(displaybox, 3, 0, 5, 3)
    29.  
    30. self.setLayout(grid)
    31. self.resize(190, 50)
    32.  
    33. def slotDial(self):
    34. x = Popen(("wvdial", "ptcl"), stdout=PIPE).stdout
    35. f = x.readline()
    36. x.close()
    37.  
    38. def showOutput(self):
    39. displaybox.append(f)
    40.  
    41. app = QtGui.QApplication(sys.argv)
    42. dg = dialerGui()
    43. dg.show()
    44. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    Any help will be much appreciated.
    Last edited by sbjaved; 6th January 2009 at 15:10. Reason: clarity

  2. #2
    Join Date
    Jan 2009
    Location
    Pakistan
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [question]redirecting stdout to display via widget?

    Here is the traceback:

    [sbjaved@pasaris Code]$ python ptcl_dialer_testing.py
    --> WvDial: Internet dialer version 1.60
    --> Warning: section [Dialer ptcl] does not exist in wvdial.conf.
    --> Cannot open /dev/modem: Device or resource busy
    --> Cannot open /dev/modem: Device or resource busy
    --> Cannot open /dev/modem: Device or resource busy
    Traceback (most recent call last):
    File "ptcl_dialer_testing.py", line 46, in showOutput
    displaybox.append(f)
    NameError: global name 'displaybox' is not defined

    This output only displays in the terminal. I would like to redirect it to the QTextBrowser widget.
    Last edited by sbjaved; 7th January 2009 at 08:35. Reason: information

  3. #3
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [question]redirecting stdout to display via widget?

    Have you considered using QProcess instead of that popen function? It provides methods to read a process's output, like QProcess::readAllStandardOutput(). Or maybe Python itself provides a way to execute external processes and return their output, like Perl or Ruby do with ``?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [question]redirecting stdout to display via widget?

    I would use a simple system() call, I'm sure Python has one. It's the most simple way to fetch output of a short running program.

  5. #5
    Join Date
    Jan 2009
    Location
    Pakistan
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [question]redirecting stdout to display via widget?

    Quote Originally Posted by wysota View Post
    I would use a simple system() call, I'm sure Python has one. It's the most simple way to fetch output of a short running program.
    The subprocess module allows you to redirect the stderr/stdout which in my case is linked to the variable 'f'. The problem I'm having is linking the variable 'f' defined in showOutput function to 'displaybox'(QTextBrowser) defined in __init__ function. Python doesn't recognize the local variable 'displaybox' (see the Traceback) and I'm stumped thinking of a way to link the output to the widget.
    In Line 21 of the code, I've linked the signal clicked() emitted by the dial QPushButton widget to the showOutput function:

    displaybox = QtGui.QTextBrowser()
    self.connect(dial, QtCore.SIGNAL('clicked()'), self.showOutput)

    Maybe something wrong here?
    Last edited by sbjaved; 8th January 2009 at 10:11. Reason: clarity

  6. #6
    Join Date
    Jan 2009
    Location
    Pakistan
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [question]redirecting stdout to display via widget?

    Quote Originally Posted by rexi View Post
    Have you considered using QProcess instead of that popen function? It provides methods to read a process's output, like QProcess::readAllStandardOutput(). Or maybe Python itself provides a way to execute external processes and return their output, like Perl or Ruby do with ``?
    I tried using QProcess following the guide posted here. But that guide is for PyQt and I couldn't get it to work. Maybe if you could post a simple PyQt4 QProcess example, it would be very helpful...
    Thanks for replying.

  7. #7
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [question]redirecting stdout to display via widget?

    Sorry, but I don't really speak enough Python to provide an example

    But looking at your code, I guess dialogbox is not a member variable of your class, but local to the __init__ method. Aren't members accessed by something like self.displaybox?

  8. #8
    Join Date
    Feb 2009
    Posts
    1
    Platforms
    Windows

    Post Re: [question]redirecting stdout to display via widget?

    Quote Originally Posted by sbjaved View Post
    I tried using QProcess following the guide posted here. But that guide is for PyQt and I couldn't get it to work. Maybe if you could post a simple PyQt4 QProcess example, it would be very helpful...
    Thanks for replying.
    Hi,

    I suffered from the same. The example you're referring to, is indeed not working flawless under PyQt4.

    I've been able to "translate" it to PyQt4. I'm using Eric4 as my IDE and created a project for the corrected version. It's working very good now...

    If you're interested in the code, let me know. It needs some more cleaning up, but I think I can manage to give it to you within a couple of days.

    Best rgds,

    --Geert

  9. #9
    Join Date
    Jan 2009
    Location
    Pakistan
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [question]redirecting stdout to display via widget?

    Quote Originally Posted by GeertVc View Post
    Hi,

    I suffered from the same. The example you're referring to, is indeed not working flawless under PyQt4.

    I've been able to "translate" it to PyQt4. I'm using Eric4 as my IDE and created a project for the corrected version. It's working very good now...

    If you're interested in the code, let me know. It needs some more cleaning up, but I think I can manage to give it to you within a couple of days.

    Best rgds,

    --Geert
    I'm very interested. Please email me the code at sbjaved AT gmail.

  10. #10
    Join Date
    Apr 2011
    Posts
    9
    Qt products
    Platforms
    Unix/X11

    Default Re: [question]redirecting stdout to display via widget?

    Hi folks!
    I'm a newbie and i'm trying to "translate" This tutorial to PyQt4.
    I don't get the output of the process on the textBrowser.
    Can anyone help me?
    Fantagol

    test1.py
    Qt Code:
    1. #!/usr/bin/python
    2. from time import sleep
    3.  
    4. print "this is"
    5. print "a test""
    6. i=0
    7. while 1:
    8. print i
    9. sleep(1)
    10. if i == 5:
    11. #os.kill(os.getpid(),signal.SIGKILL)
    12. i +=1
    To copy to clipboard, switch view to plain text mode 

    CapturingOutputfromProcess.py
    Qt Code:
    1. import sys,os,signal
    2. from PyQt4 import QtCore, QtGui
    3.  
    4. class Window(QtGui.QWidget):
    5. def __init__(self):
    6. #Layout
    7. QtGui.QWidget.__init__(self)
    8. self.textBrowser = QtGui.QTextBrowser(self)
    9. self.lineEdit = QtGui.QLineEdit(self)
    10. self.startButton = QtGui.QPushButton("Start", self)
    11. self.stopButton = QtGui.QPushButton("Stop", self)
    12. self.stopButton.setEnabled(False)
    13. layout = QtGui.QGridLayout(self)
    14. layout.setSpacing(8)
    15. layout.addWidget(self.textBrowser, 0, 0)
    16. layout.addWidget(self.lineEdit, 1, 0)
    17. layout.addWidget(self.startButton, 1, 1)
    18. layout.addWidget(self.stopButton, 1, 2)
    19. #Connection
    20. self.connect(self.lineEdit, QtCore.SIGNAL("returnPressed()"), self.startCommand)
    21. self.connect(self.startButton, QtCore.SIGNAL("clicked()"), self.startCommand)
    22. self.connect(self.stopButton, QtCore.SIGNAL("clicked()"), self.stopCommand)
    23. #Process
    24. self.process = QtCore.QProcess()
    25. self.connect(self.process, QtCore.SIGNAL("readyReadStdout()"), self.readOutput)
    26. self.connect(self.process, QtCore.SIGNAL("readyReadStderr()"), self.readErrors)
    27. self.connect(self.process, QtCore.SIGNAL("processExited()"), self.resetButtons)
    28.  
    29. def startCommand(self):
    30. self.process.start("python test1.py", QtCore.QStringList(["my_file.tex"]))
    31. StartString="Process started with pid: "+str(self.process.pid())
    32. print StartString
    33. self.textBrowser.append(StartString)
    34. print self.process.ProcessState()
    35. #self.process.close()
    36. self.startButton.setEnabled(False)
    37. self.stopButton.setEnabled(True)
    38. #self.textBrowser.clear()
    39.  
    40. #~ if not self.process.start():
    41. #~ self.textBrowser.setText(
    42. #~ QtGui.QString("*** Failed to run %1 ***").arg(self.lineEdit.text())
    43. #~ )
    44. #~ self.resetButtons()
    45. #~ return
    46. def stopCommand(self):
    47. self.resetButtons()
    48. print "Stop process, PID was: ", self.process.pid()
    49. os.kill(self.process.pid(), signal.SIGKILL)
    50. #QtCore.QTimer.singleShot(5000, self.process, SLOT("kill()"))
    51.  
    52. def readOutput(self):
    53. print self.process.readStdout()
    54. self.textBrowser.append(QtGui.QString(self.process.readStdout()))
    55. if self.process.isRunning()==False:
    56. self.textBrowser.append("\n Completed Successfully")
    57. def readErrors(self):
    58. self.textBrowser.append("error: " + QtGui.QString(self.process.readLineStderr()))
    59. def resetButtons(self):
    60. self.startButton.setEnabled(True)
    61. self.stopButton.setEnabled(False)
    62.  
    63. if __name__ == "__main__":
    64. app = QtGui.QApplication(sys.argv)
    65.  
    66. window = Window()
    67. window.show()
    68.  
    69. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [question]redirecting stdout to display via widget?

    First you read output from the process and print it to the screen and then you read it again and try to add it to the widget. Since you have already read the output, nothing remains to be read and you don't get anything in your widget.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Apr 2011
    Posts
    9
    Qt products
    Platforms
    Unix/X11

    Default Re: [question]redirecting stdout to display via widget?

    Tks 1k wysota, for your answer, but if I change readOutput in
    Qt Code:
    1. def readOutput(self):
    2. print self.process.readStdout()
    To copy to clipboard, switch view to plain text mode 
    I don't get any output too, and futher the process is not running,
    according to QProcess.ProcessState = 0.

    bye
    Fantagol

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [question]redirecting stdout to display via widget?

    When are you reading the state? Just after calling start()? If so then the process is not started yet at that point. Either way make sure you pass correct arguments to start(). To me it seems "python" is the name of your program and "test1.py" and "my_file.tex" are its arguments. Unless of course you do have an executable called "python test1.py" but that would be weird.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Apr 2011
    Posts
    9
    Qt products
    Platforms
    Unix/X11

    Default Re: [question]redirecting stdout to display via widget?

    Thanks very much for your precious help wysota, but nothing happens.This is what I did:

    According to this Documentation

    I add this connection
    Qt Code:
    1. self.connect(self.process, QtCore.SIGNAL("started()"), self.processIsStarted)
    To copy to clipboard, switch view to plain text mode 

    add a new function:

    Qt Code:
    1. def processIsStarted(self):
    2. print "according to SIGNAL STARTED() process with pid: ",str(self.process.pid()),"is started"
    3. print "Its State is ", self.process.ProcessState()
    To copy to clipboard, switch view to plain text mode 
    and change the calling:
    Qt Code:
    1. def startCommand(self):
    2. self.process.start("python", QtCore.QStringList(["test1.py","my_file.txt"]))
    3. StartString="Process started with pid: "+str(self.process.pid())
    4. print StartString
    5. self.textBrowser.append(StartString)
    6. print self.process.ProcessState()
    7. self.startButton.setEnabled(False)
    8. self.stopButton.setEnabled(True)
    To copy to clipboard, switch view to plain text mode 
    To starts a given program, i have to use this instantiation?
    Qt Code:
    1. QProcess.start (self, QString program, QStringList arguments, QIODevice.OpenMode mode = QIODevice.ReadWrite)
    To copy to clipboard, switch view to plain text mode 
    Is QIODevice.OpenMode important?

    tks to all
    fantagol
    Last edited by fantagol; 20th April 2011 at 14:25.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [question]redirecting stdout to display via widget?

    No, the mode is not important for you. Does it work if you change test1.py to an absolute path? It is likely your file cannot be found. Although in this case python SHOULD start, it would just quit immediately. Unless your python binary cannot be found as well.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #16
    Join Date
    Apr 2011
    Posts
    9
    Qt products
    Platforms
    Unix/X11

    Default Re: [question]redirecting stdout to display via widget?

    Here the new files:
    test1.py
    CapturingOutputfromProcess.py

    I think that the python script "test1.py" is started by my code, but self.process.ProcessState() say me quite another thing. I change the test1.py with a simple infinite print cycle:
    Qt Code:
    1. #!/usr/bin/python
    2. print "test1"
    3. print "test2"
    4. #from time import sleep
    5. i=0
    6. while 1:
    7. print i
    8. #sleep(1)
    9. if i == 5:
    10. #os.kill(os.getpid(),signal.SIGKILL)
    11. pass
    12. i +=1
    To copy to clipboard, switch view to plain text mode 

    I think that the new process is started because i can see its pid on "System Monitor", and its cpu occupation. This is the new attemp in the script:


    Qt Code:
    1. def startCommand(self):
    2. self.process.start("python", QtCore.QStringList(["/home/MyUserAccount/test1.py"]))
    3. StartString="Process started with pid: "+str(self.process.pid())
    4. print StartString
    5. self.textBrowser.append(StartString)
    6. print self.process.ProcessState()
    7. #self.process.close()
    8. self.startButton.setEnabled(False)
    9. self.stopButton.setEnabled(True)
    10. #self.textBrowser.clear()
    11.  
    12. def processIsStarted(self):
    13. print "according to SIGNAL STARTED() process with pid: ",self.process.pid()
    14.  
    15. def stopCommand(self):
    16. print "Attempt to stop a process with Pid: ", str(self.process.pid())
    17. print "What is the State of this process?"
    18. print "Its State is ", self.process.ProcessState()
    19. self.resetButtons()
    20. print "Stop process, PID was: ", self.process.pid()
    21. os.kill(self.process.pid(), signal.SIGKILL)
    To copy to clipboard, switch view to plain text mode 

    When i run $python CapturingOutputfromProcess03.py , and press the button start, the message is shown correctly on the textbrowser "Process started with pid: 2277":
    Qt Code:
    1. Process started with pid: 2277
    2. 0
    3. according to SIGNAL STARTED() process with pid: 2277
    To copy to clipboard, switch view to plain text mode 
    A new pid appears on the System Monitor with its CPU occupation.
    Then, when I press the stop button, the gui disappears and the terminal reveals this:
    Qt Code:
    1. Attempt to stop a process with Pid: 2283
    2. What is the State of this process?
    3. Its State is 0
    4. Stop process, PID was: 2283
    To copy to clipboard, switch view to plain text mode 

    I hope we can reach a solution.
    Thanks a lot
    Last edited by fantagol; 20th April 2011 at 22:33.

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [question]redirecting stdout to display via widget?

    But what's wrong exactly? It seems the script gets executed, what more do you want?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #18
    Join Date
    Apr 2011
    Posts
    9
    Qt products
    Platforms
    Unix/X11

    Default Re: [question]redirecting stdout to display via widget?

    Quote Originally Posted by wysota View Post
    But what's wrong exactly? It seems the script gets executed, what more do you want?
    I want to redirect the standard output, and print on the textBrowser what comes from test1.py. I don't see anything append on the widget by test1.py.

  19. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [question]redirecting stdout to display via widget?

    Then connect to the appropriate signal and read the output from within the slot.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  20. #20
    Join Date
    Apr 2011
    Posts
    9
    Qt products
    Platforms
    Unix/X11

    Default Re: [question]redirecting stdout to display via widget?

    I'm looking for the solution from many days!!
    This is the SIGNAL:

    Qt Code:
    1. self.connect(self.process, QtCore.SIGNAL("readyReadStdout()"), self.readOutput)
    2. self.connect(self.process, QtCore.SIGNAL("readyReadStderr()"), self.readErrors)
    To copy to clipboard, switch view to plain text mode 
    and this is the SLOT:

    Qt Code:
    1. def readOutput(self):
    2. #print self.process.readStdout()
    3. self.textBrowser.append(QtGui.QString(self.process.readStdout()))
    4. #if self.process.isRunning()==False:
    5. # self.textBrowser.append("\n Completed Successfully")
    6. def readErrors(self):
    7. self.textBrowser.append("error: " + QtGui.QString(self.process.readLineStderr()))
    To copy to clipboard, switch view to plain text mode 

    ....Where is the mistake?

Similar Threads

  1. QDockWidget inside another widget in the center?
    By Antebios in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 07:06
  2. How to display selected columns in QTableView widget.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 08:30
  3. Replies: 4
    Last Post: 10th March 2007, 18:01
  4. QWidget display on 2 stack widget page
    By spawnwj in forum Qt Programming
    Replies: 3
    Last Post: 4th September 2006, 12:07
  5. Replies: 3
    Last Post: 12th April 2006, 08:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.