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 include readQVariantList
and writeQVariantList
for reading and writing lists from a datastream:
>>> from PyQt5 import QtCore
>>> f = QtCore.QFile('test.dat')
>>> f.open(QtCore.QIODevice.ReadWrite)
True
>>> stream = QtCore.QDataStream(f)
>>> x = [3.142, 'foo', 7, QtCore.QPoint(3, 5), QtCore.QDate.currentDate()]
>>> stream.writeQVariantList(x)
>>> f.seek(0)
True
>>> stream.readQVariantList()
[3.142, 'foo', 7, PyQt5.QtCore.QPoint(3, 5), PyQt5.QtCore.QDate(2023, 4, 6)]
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).