Issue
I am trying to subclass QFile in PySide to implement custom read behavior. However, as seen in the simplified code below, even if the subclass' readData implementation just calls the parent's readData function, the returned data is incorrect. Subclassing other QIODevices such as QBuffer also causes incorrect return values. Has anyone successfully subclassed a QIODevice?
from PySide import QtCore
class FileChild1(QtCore.QFile):
pass
class FileChild2(QtCore.QFile):
def readData(self, maxlen):
return super(FileChild2, self).readData(maxlen)
f1 = FileChild1('test.txt')
f1.open(QtCore.QIODevice.ReadWrite|QtCore.QIODevice.Truncate)
f1.write('Test text for testing')
f1.seek(0)
print 'FileChild1: ', repr(f1.read(50))
f2 = FileChild2('test2.txt')
f2.open(QtCore.QIODevice.ReadWrite|QtCore.QIODevice.Truncate)
f2.write('Test text for testing')
f2.seek(0)
print 'FileChild2: ', repr(f2.read(50))
>>> FileChild1: PySide.QtCore.QByteArray('Test text for testing')
>>> FileChild2: PySide.QtCore.QByteArray('─ Q ►│A☻ @ p¼a☻Test text for testing\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
Solution
I tested your script with PyQt 4.8 and PyQt 4.9 with Python 2.7.2 / Qt 4.8.0, and in both cases it produces the following output:
FileChild1: 'Test text for testing'
FileChild2: 'Test text for testing'
So readData
returns a byte string, as per the PyQt4 docs.
Using PySide 1.0.9 with Python 2.7.2 / Qt 4.8.0, I get this output:
FileChild1: PySide.QtCore.QByteArray('Test text for testing')
FileChild2: PySide.QtCore.QByteArray('')
Not sure why there is a difference in return type between PyQt4 and PySide, but there is clearly some kind of bug in PySide.
There is a bug report here which looks like it may be somewhat related, but it's not particularly recent (PySide 1.0.7).
Answered By - ekhumoro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.