Details
Description
Passing a QIODevice.WriteOnly object as an argument into QDatastream.setDevice() results in the Qdatastream object not recieving a proper file pointer. Here is some code under PySide2:
from PySide2 import QtCore,QtGui,QtWidgets import time #we will serialize the data into the file file_ = QtCore.QFile("asdf.dat") file_.open(QtCore.QIODevice.WriteOnly) out = QtCore.QDataStream() out.setDevice(file_) out.writeInt32(123) file_ = QtCore.QFile("asdf.dat") file_.open(QtCore.QIODevice.ReadOnly) # read the data serialized from the file i = QtCore.QDataStream() i.setDevice(file_) a = i.readInt32() print("value read from file.dat: {}".format(a))
This results in an incorrect output:
value read from file.dat: 0
Here is a version of the code (importing pyqt5)
from PyQt5 import QtCore,QtGui,QtWidgets import time #we will serialize the data into the file file_ = QtCore.QFile("asdf.dat") file_.open(QtCore.QIODevice.WriteOnly) out = QtCore.QDataStream() out.setDevice(file_) out.writeInt32(123) file_ = QtCore.QFile("asdf.dat") file_.open(QtCore.QIODevice.ReadOnly) # read the data serialized from the file i = QtCore.QDataStream() i.setDevice(file_) a = i.readInt32() print("value read from file.dat: {}".format(a))
This code results in the correct output:
C:\Projects\pyside_tests>python datastream_test.py
value read from file.dat: 123