我试图在PySide中继承QFile来实现自定义读取行为。但是,如下面的简化代码所示,即使子类'readData实现只调用父的readData函数,返回的数据也是不正确的。对QBuffer等其他QIODevices进行子类化也会导致返回值不正确。有没有人成功地将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')
答案 0 :(得分:2)
我使用PyQt 4.8测试了你的脚本,使用Python 2.7.2 / Qt 4.8.0测试了PyQt 4.9,在这两种情况下它产生了以下输出:
FileChild1: 'Test text for testing'
FileChild2: 'Test text for testing'
所以readData
根据PyQt4 docs返回一个字节字符串。
使用PySide 1.0.9和Python 2.7.2 / Qt 4.8.0,我得到了这个输出:
FileChild1: PySide.QtCore.QByteArray('Test text for testing')
FileChild2: PySide.QtCore.QByteArray('')
不确定为什么PyQt4和PySide之间的返回类型存在差异,但PySide中显然存在某种错误。
有一个错误报告here看起来可能有些相关,但它并不是特别近期(PySide 1.0.7)。
答案 1 :(得分:1)
PySide的错误来自shiboken:qint64在QIODevice中用作偏移类型,但qint64在Python 2.x中映射到“int”,而不是“long”。当qint64的值大于qint32时,读取该值将导致Python 2.x抛出OverflowError。 当使用qint64作为Slot / Signal / Property或任何Qt元类型与Qt C ++代码与Python通信时,会发生类似的OverflowError。
我也在寻找绕过这个问题的解决方案。
答案 2 :(得分:0)
在Qt5和PySide2上也非常有效。我们正在调查。