I have a QtDataStream:
from PyQt5 import QtCore
infile = QtCore.QFile(path)
if not infile.open(QtCore.QIODevice.ReadOnly):
raise IOError(infile.errorString()
stream = QtCore.QDataStream(infile)
And I need to read a QVector. I need something like this:
var = QtCore.Qvector()
stream >> var
But it seems that QVector class does not exist in PyQt5. Any ideas?
PyQt cannot wrap QVector, because it is a template class. Such classes are part of the C++ language, and have no equivalent in Python. To work around this limitation, PyQt has added some special wrapper methods to the QDataStream class which provide the same functionality in a different way.
These methods are only mentioned in the PyQt documentation - so this is one of those rare cases where you must consult the PyQt docs rather than the Qt ones. The special wrapper methods are all named using the format
read/write[TypeName], and includereadQVariantListandwriteQVariantListfor reading and writing lists from a datastream:In addition to this, there is an alternative, lower level way to read data from template classes like QVector and QList, which involves reading the length of the container first and then reading each element one-by-one (see this answer for more details). But this will still only result in a Python list - there's no way to create an instance of QVector or QList in PyQt (and there's never any need to do so).