[PySide] Using QTextStream with stdin

Aaron Richiger a.richi at bluewin.ch
Sat Jul 21 19:12:51 CEST 2012


Hello John

I really think, the documentation of PySide is wrong here, the examples 
are not even valid python syntax...
First of all, I can not find a reason why not to use the convenient 
python functions print and raw_input() to read/write to the console. I 
would use this one or sys.stdin/ sys.stdout as usual in python apps 
without PySide. Using QTextStream might be a good idea in C++, but in 
python I can not see any advantage. I found the solution to use 
QTextStream with PySide and wrote a little application that lets you 
easily compare both variants (see code below). Run the script from the 
console and then you can read/write to this console after using the 
pushbuttons in the widget. You will see soon the disadvantages of using 
QTextStream for reading: you have to terminate your input by pressing 
ENTER and then CTRL-D instead of just pressing ENTER and the widget 
freezes after a few seconds, but will get back if you terminate your 
input. This disadvantages do not appear if you simply use pythons 
raw_input()...
I hope this helps you to get started!

Cheers, Aaron
Code:

############################

from PySide.QtGui import *
from PySide.QtCore import *


class Widget(QWidget):

     def __init__(self, parent=None):
         super(Widget, self).__init__(parent)
         self.setupUi()
         self.setupButtonSlots()
         self.setupStreams()

     def setupUi(self):
         self.setLayout(QVBoxLayout())
         self.pbReadPython = QPushButton("Read console input (using 
python's raw_input())", self)
         self.labelReadPython = QLabel('Your console input will be shown 
here: ', self)
         self.pbReadQt = QPushButton("Read console input (using 
QTextstream)", self)
         self.labelReadQt = QLabel('Your console input will be shown 
here: ', self)
         self.pbWritePython = QPushButton("Write to console (using 
python's print)", self)
         self.pbWriteQt = QPushButton("Write to console (using 
QTextstream)", self)

         self.layout().addWidget(self.pbReadPython)
         self.layout().addWidget(self.labelReadPython)
         self.layout().addWidget(self.pbReadQt)
         self.layout().addWidget(self.labelReadQt)
         self.layout().addWidget(self.pbWritePython)
         self.layout().addWidget(self.pbWriteQt)

     def setupButtonSlots(self):
         self.pbReadPython.clicked.connect(self.readPython)
         self.pbReadQt.clicked.connect(self.readQt)
         self.pbWritePython.clicked.connect(self.writePython)
         self.pbWriteQt.clicked.connect(self.writeQt)

     def setupStreams(self):
         self.stdin = QFile()
         self.stdin.open(0, 
QIODevice.OpenMode(QIODevice.OpenModeFlag.ReadOnly))
         self.stdout = QFile()
         self.stdout.open(1, 
QIODevice.OpenMode(QIODevice.OpenModeFlag.WriteOnly))

         self.inStream = QTextStream(self.stdin)
         self.outStream = QTextStream(self.stdout)

     def readPython(self):
         txt = raw_input('Write something and press ENTER: ')
         print 'Thanks, your input is shown in the widget.'
         self.labelReadPython.setText('Your input was: %s' % txt)

     def readQt(self):
         print "Write something and press ENTER and then CTRL-D to start 
reading: "
         txt = self.inStream.readLine()
         print "Thanks, your input is shown in the widget."
         self.labelReadQt.setText('Your input was: %s' % txt)

     def writePython(self):
         print "This is some test text written to console using python's 
print."

     def writeQt(self):
         self.outStream << "This is some test text written to console 
using QTextstream."
         self.outStream << 10    # new line
         self.outStream.flush()


if __name__ == '__main__':
     import sys
     app = QApplication(sys.argv)
     w = Widget()
     w.show()
     sys.exit(app.exec_())















Am 21.07.2012 05:02, schrieb John Tourtellott:
> The QTextStream documentation states that it is common to use 
> PySide.QtCore.QTextStream to read console input, however, I get a 
> python error when I try the example in the documentation:
>
>     stream = QTextStream(sys.stdin.fileno())
>
>
> I'll paste the full error message below, but basically QTextStream has 
> no constructor that takes in an integer (which the file descriptor 
> is). How do you use QTextStream to read console input?
>
> Python error message:
>
>     TypeError: 'PySide.QtCore.QTextStream' called with wrong argument
>     types:
>     PySide.QtCore.QTextStream(int)
>     Supported signatures:
>     PySide.QtCore.QTextStream()
>     PySide.QtCore.QTextStream(PySide.QtCore.QByteArray,
>     PySide.QtCore.QIODevice.OpenMode = QIODevice.ReadWrite)
>     PySide.QtCore.QTextStream(PySide.QtCore.QIODevice)
>
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/pyside


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20120721/ce74cb8e/attachment.html>


More information about the PySide mailing list