PySide 从未提供过像 QString、QStringList、QVariant 等这样的类。它总是对等价的 python 类型进行隐式转换——也就是说,在 PyQt 术语中,它只实现了v2 API(参见PSEP 101了解更多详情)。
然而,与 PyQt4 相比,QProcess 在尝试编写 unicode 字符串时的行为在 PySide 中似乎有些问题。这是 PyQt4 中的一个简单测试:
Python 2.7.8 (default, Sep 24 2014, 18:26:21)
[GCC 4.9.1 20140903 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from PyQt4 import QtCore
>>> QtCore.PYQT_VERSION_STR
'4.11.2'
>>> p = QtCore.QProcess()
>>> p.start('cat'); p.waitForStarted()
True
>>> p.write(u'fóó'); p.waitForReadyRead()
3L
True
>>> p.readAll()
PyQt4.QtCore.QByteArray('f\xf3\xf3')
因此,PyQt 似乎会将 unicode 字符串隐式编码为“latin-1”,然后再将它们传递给QProcess.write()(当然需要const char * 或QByteArray)。如果您想要不同的编码,则必须明确完成:
>>> p.write(u'fóó'.encode('utf-8')); p.waitForReadyRead()
5L
True
>>> p.readAll()
PyQt4.QtCore.QByteArray('f\xc3\xb3\xc3\xb3')
现在让我们看看 PySide 会发生什么:
Python 2.7.8 (default, Sep 24 2014, 18:26:21)
[GCC 4.9.1 20140903 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from PySide import QtCore, __version__
>>> __version__
'1.2.2'
>>> p = QtCore.QProcess()
>>> p.start('cat'); p.waitForStarted()
True
>>> p.write(u'fóó'); p.waitForReadyRead()
0L
^C
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyboardInterrupt
所以:没有隐式编码,进程只是阻塞而不是引发错误(这似乎是一个错误)。但是,使用显式编码重新尝试按预期工作:
>>> p.start('cat'); p.waitForStarted()
True
>>> p.write(u'fóó'.encode('utf-8')); p.waitForReadyRead()
5L
True
>>> p.readAll()
PySide.QtCore.QByteArray('fóó')