[PyQt] Interfacing with binary files: QDataStream issue

Elvis Stansvik elvstone at gmail.com
Tue Jun 20 07:33:48 BST 2017


Den 19 juni 2017 9:09 em skrev "Christopher Probst" <
christop.probst at gmail.com>:
>
> Hi,
>
> I am exploring interfacing of files with QDataStream. But it does not
seem to work. To write to a file I do this:
>
>
> file = QFile("file.dat")
> color = QColor(Qt.red)
>
> if file.open(QIODevice.WriteOnly):
>  out = QDataStream(file)
>  out << color
>  file.flush()
>  file.close()
>
> It looks as if that the data gets written to the file. It is the reading
where there is a problem:
>
> file = QFile("file.dat")
> color = QVariant()
>
>
> if file.open(QIODevice.ReadOnly):
>  out = QDataStream(file)
>  out >> color

I haven't used QDataStream with PyQt, but this looks strange to me: How
will it know that it's a QColor you want to deserialise if color is a
QVariant? I think it needs to be a QColor for there to be any chance of
this working.

Hope that helps.

Elvis

>  print(color)
>  print(color.value())
>
> The output is:
>
> <PyQt5.QtCore.QVariant object at 0x7f2cdd556f28> None
>
> What am I doing wrong?
>
> Thanks, Christopher
>
>
>
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20170620/70ae26d4/attachment.html>


More information about the PyQt mailing list