<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p><br>
</p>
<br>
<div class="moz-cite-prefix">On 06/19/2017 09:08 PM, Christopher
Probst wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CABhWjno+8UCyfgTa2GTEojt0if3DNFGGy0ij2P-W=LO2YxfqLw@mail.gmail.com">
<div dir="ltr">Hi,
<div><br>
</div>
<div>I am exploring interfacing of files with QDataStream. But
it does not seem to work. To write to a file I do this:</div>
<div><span
id="gmail-docs-internal-guid-16892120-c1bc-3d7c-18bd-a2f3b94e7f7e">
<p dir="ltr"
style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="font-size:13pt;font-family:Arial;color:rgb(0,0,0);background-color:transparent;vertical-align:baseline;white-space:pre-wrap">
</span></p>
<pre style="color:rgb(0,0,0);font-family:"DejaVu Sans Mono";font-size:11.4pt">file = QFile(<span style="color:rgb(0,128,128);font-weight:bold">"file.dat"</span>)
color = QColor(Qt.red)
<span style="color:rgb(0,0,128);font-weight:bold">if </span>file.open(QIODevice.WriteOnly):
out = QDataStream(file)
out << color
file.flush()
file.close()
</pre>
<p style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="background-color:transparent;font-size:13pt;font-family:Arial;color:rgb(128,128,128);font-style:italic;vertical-align:baseline;white-space:pre-wrap"><span style="color:rgb(0,0,0);font-size:17.3333px;font-style:normal">It looks as if that the data gets written to the file. It is the reading where there is a problem:
</span></span></p>
<pre style="color:rgb(0,0,0);font-family:"DejaVu Sans Mono";font-size:11.4pt">file = QFile(<span style="color:rgb(0,128,128);font-weight:bold">"file.dat"</span>)
color = QVariant()
<span style="color:rgb(0,0,128);font-weight:bold">if </span>file.open(QIODevice.ReadOnly):
out = QDataStream(file)
out >> color
<span style="color:rgb(0,0,128)">print</span>(color)
<span style="color:rgb(0,0,128)">print</span>(color.value())
</pre>
<pre style="color:rgb(0,0,0);font-family:"DejaVu Sans Mono";font-size:11.4pt"><pre style="font-family:"DejaVu Sans Mono";font-size:11.4pt">The output is: </pre></pre>
<pre style="color:rgb(0,0,0);font-family:"DejaVu Sans Mono";font-size:11.4pt"><PyQt5.QtCore.QVariant object at 0x7f2cdd556f28>
None</pre>
<pre style="color:rgb(0,0,0);font-family:"DejaVu Sans Mono";font-size:11.4pt">What am I doing wrong?</pre>
<pre style="color:rgb(0,0,0);font-family:"DejaVu Sans Mono";font-size:11.4pt">Thanks,
Christopher</pre>
<pre style="color:rgb(0,0,0);font-family:"DejaVu Sans Mono";font-size:11.4pt">
</pre>
<p style="line-height:1.2;margin-top:0pt;margin-bottom:0pt"><span style="background-color:transparent;font-size:13pt;font-family:Arial;color:rgb(128,128,128);font-style:italic;vertical-align:baseline;white-space:pre-wrap"><span style="color:rgb(0,0,0);font-size:17.3333px;font-style:normal">
</span></span></p>
</span></div>
</div>
</blockquote>
Is there a specific reason you're using binary files to store the
information? It's probably a lot easier for you to use something
like python's built-in json module to store files.
(<a class="moz-txt-link-freetext" href="https://docs.python.org/3/library/json.html">https://docs.python.org/3/library/json.html</a>)<br>
<br>
Here's an example which would work with your code:<br>
<br>
<blockquote>import json<br>
from PyQt5 import QtGui<br>
<br>
def write_(value):<br>
if isinstance(value, QtGui.QColor):<br>
return {'__type__': 'QColor', 'value': value.rgba()}<br>
raise TypeError('Unknown type:', type(value))<br>
<br>
def read_(entry):<br>
if '__type__' in entry and entry['__type__'] == 'QColor':<br>
return QtGui.QColor.fromRgba(entry['value'])<br>
return entry<br>
<br>
<br>
def main():<br>
colors = {<br>
'red': QtGui.QColor.fromRgb(1.0, 0.0, 0.0),<br>
'green': QtGui.QColor.fromRgb(0.0, 1.0, 0.0),<br>
'blue': QtGui.QColor.fromRgb(0.0, 0.0, 1.0),<br>
}<br>
<br>
with open('my_file.dat', 'w') as my_file:<br>
my_file.write(json.dumps(colors, default=write_))<br>
<br>
with open('my_file.dat', 'r') as my_file:<br>
colors_read = json.loads(my_file.read(),
object_hook=read_)<br>
print(colors_read)<br>
<br>
<br>
if __name__ == '__main__':<br>
main()<br>
</blockquote>
<br>
</body>
</html>