[PyQt] QDataStream and template classes

ekhumoro ekhumoro at gmx.com
Sat Dec 10 20:35:10 GMT 2016


On 26/11/16 18:09, Phil Thompson wrote:
> On 26 Nov 2016, at 5:38 pm, ekhumoro <ekhumoro at gmx.com> wrote:
>>
>> If I have received a file serialized by QDatastream that uses
>> template classes such as QList<double> and QVector<QPoint>, am I
>> right in saying there is no way to read it using PyQt?
>>
>> I see there is readQVariantList which can read QList<QVariant>, but
>> there does not seem any general way to handle template classes. I
>> suppose it's not surprising that PyQt can't do this, but I just
>> wanted to confirm there are no work-arounds.
>
> I can't think of any.
>
> Phil

Well, it seems there is a way to do this using the existing methods. It 
just requires some knowledge of the datastream format:

     http://doc.qt.io/qt-4.8/datastreamformat.html

Fortunately it is quite simple, so a QVector<QPoint> can be read like this:

     points = []
     length = stream.readUInt32()
     for index in range(length):
         point = QPoint()
         stream >> point
         points.append(point)


More information about the PyQt mailing list