Results 1 to 20 of 27

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.


  3. #3
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.


  5. #5
    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.


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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.


  9. #9
    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?

  10. #10
    Join Date
    Feb 2008
    Posts
    491
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 142 Times in 135 Posts

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

    As Wysota says:
    connect to the appropriate signal
    QProcess::readyReadStdout() is a PyQt3 signal. Check all of your functions against the doc that you linked in post #14.

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

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

    Quote Originally Posted by fantagol View Post
    ....Where is the mistake?
    The mistake is you are trying to use a signal that doesn't exist.
    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?

    Yeah.... i found the mistake this morning, The correct signals are:
    Qt Code:
    1. readyReadStandardOutput()
    2. readyReadStandardError()
    To copy to clipboard, switch view to plain text mode 
    but now i read the text printed by test1.py only when the process is terminated.
    I'd like to read line by line while it is running

  13. #13
    Join Date
    Feb 2008
    Posts
    491
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    12
    Thanked 142 Times in 135 Posts

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

    A forum search yielded a similar thread.

    For Python read the manual page: man python

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
  •  
Qt is a trademark of The Qt Company.