[PyQt] Interfacing with binary files: QDataStream issue

Phil Thompson phil at riverbankcomputing.com
Wed Jun 21 17:18:49 BST 2017


On 19 Jun 2017, at 8:08 pm, Christopher Probst <christop.probst at gmail.com> wrote:
> 
> 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
>    print(color)
>    print(color.value())
> The output is: 
> <PyQt5.QtCore.QVariant object at 0x7f2cdd556f28>
> None
> 
> What am I doing wrong?

Instead of...

color = QVariant()

...do...

color = QColor()

Phil



More information about the PyQt mailing list