<div dir="ltr"><div style="font-family:tahoma,sans-serif" class="gmail_default"></div><div class="gmail_default" style="font-family:tahoma,sans-serif">I am finding (in PyQt 5.7 at least) that sub-classing <span style="font-family:monospace,monospace">QSqlQueryModel</span> and overriding its <span style="font-family:monospace,monospace">data()</span> method produces an incorrect result when the value retrieved from a MySQL database is <span style="font-family:monospace,monospace">NULL</span>.</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">Full details are in <a href="https://forum.qt.io/topic/90363/inexplicable-qsqlquerymodel-handling-of-null-value">https://forum.qt.io/topic/90363/inexplicable-qsqlquerymodel-handling-of-null-value</a>, and particularly post # <a href="https://forum.qt.io/topic/90363/inexplicable-qsqlquerymodel-handling-of-null-value">https://forum.qt.io/topic/90363/inexplicable-qsqlquerymodel-handling-of-null-value</a>. Nobody has tried it in C++ for me to date to verify, but I'm suspecting this might be a PyQt bug?</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">Briefly:</div><div class="gmail_default" style="font-family:tahoma,sans-serif">My <span style="font-family:monospace,monospace">SELECT</span> query returns a column which is NULLable, and has <span style="font-family:monospace,monospace">NULL</span> as its value. Where I expect "blank" as the end value, I actually get, for example, <span style="font-family:monospace,monospace">0</span> if the column type is <i>int</i> or <span style="font-family:monospace,monospace">''</span> if the type is <i>string</i>, etc.</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">This is when I sub-class <span style="font-family:monospace,monospace">QSqlQueryModel</span>. If all I have is:</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><pre style="background-color:rgb(255,255,255);color:rgb(0,0,0);font-family:"DejaVu Sans Mono";font-size:9pt"><span style="color:rgb(0,0,128);font-weight:bold">class </span>DBQueryModel(QtSql.QSqlQueryModel):<br> <span style="color:rgb(0,0,128);font-weight:bold">def </span><span style="color:rgb(178,0,178)">__init__</span>(<span style="color:rgb(148,85,141)">self</span>, parent=<span style="color:rgb(0,0,128);font-weight:bold">None</span>):<br> <span style="color:rgb(0,0,128)">super</span>().<span style="color:rgb(178,0,178)">__init__</span>(parent)</pre>I get the "NULL"/"blank". However, as soon as I add just:</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><pre style="background-color:rgb(255,255,255);color:rgb(0,0,0);font-family:"DejaVu Sans Mono";font-size:9pt"><span style="color:rgb(0,0,128);font-weight:bold">def </span>data(<span style="color:rgb(148,85,141)">self</span>, index: QtCore.QModelIndex, role=QtCore.Qt.DisplayRole) -> typing.Any:<br> <span style="color:rgb(0,0,128);font-weight:bold">return </span><span style="color:rgb(0,0,128)">super</span>().data(index, role)</pre>I get those values instead of NULL.</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">Note that my override is based on the Qt definition of the method at <a href="http://doc.qt.io/qt-5/qsqlquerymodel.html#data">http://doc.qt.io/qt-5/qsqlquerymodel.html#data</a>:</div><div class="gmail_default" style="font-family:tahoma,sans-serif"></div><div class="gmail_default" style="font-family:tahoma,sans-serif"><h3 class="gmail-fn" id="gmail-data"><span class="gmail-type"><a href="http://doc.qt.io/qt-5/qvariant.html">QVariant</a></span> QSqlQueryModel::<span class="gmail-name">data</span>(const <span class="gmail-type"><a href="http://doc.qt.io/qt-5/qmodelindex.html">QModelIndex</a></span> &<i>item</i>, <span class="gmail-type">int</span> <i>role</i> = Qt::DisplayRole) const</h3>Note that the default for <span style="font-family:monospace,monospace">role</span> is <span style="font-family:monospace,monospace">Qt::DisplayRole</span>. However, in <span style="font-family:monospace,monospace">QtSql.py</span> I see:</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><pre style="background-color:rgb(255,255,255);color:rgb(0,0,0);font-family:"DejaVu Sans Mono";font-size:9pt"><span style="color:rgb(0,0,128);font-weight:bold">def </span>data(<span style="color:rgb(148,85,141)">self</span>, QModelIndex, role=<span style="color:rgb(0,0,128);font-weight:bold">None</span>): <span style="color:rgb(128,128,128);font-style:italic"># real signature unknown; restored from __doc__<br></span><span style="color:rgb(128,128,128);font-style:italic"> """ data(self, QModelIndex, role: int = Qt.DisplayRole) -> Any """<br></span><span style="color:rgb(128,128,128);font-style:italic"> </span><span style="color:rgb(0,0,128);font-weight:bold">pass</span></pre> You will notice that the <i>comment</i> shows the default should be<span style="color:rgb(128,128,128);font-style:italic"> Qt.DisplayRole</span>, but the declaration defaults it to<span style="color:rgb(0,0,128);font-weight:bold"> None</span> instead.<br></div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">I don't know enough to be sure, but would that be the underlying cause of the unexpected behaviour?</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">FWIW, I have tried making my override be:</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><pre style="background-color:rgb(255,255,255);color:rgb(0,0,0);font-family:"DejaVu Sans Mono";font-size:9pt"><span style="color:rgb(0,0,128);font-weight:bold">def </span>data(<span style="color:rgb(148,85,141)">self</span>, index: QtCore.QModelIndex, role=None)</pre></div><div class="gmail_default" style="font-family:tahoma,sans-serif">instead, but same bad behaviour.</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">1. Is this indeed a bug in PyQt, and the cause of my issue?</div><div class="gmail_default" style="font-family:tahoma,sans-serif">2. If so, I presume you (Phil!) will be kind enough to fix. However, for my part I am stuck with PyQt 5.7 for the foreseeable future. If the fix is indeed to change code in the latest/next release, is there anything I can do in existing code (my override) to make it work in 5.7, as a workaround? (in real code I need the override, as I do other processing)</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">My coding has come to halt as I cannot proceed without a fix. So I should be obliged for any early response as to whether this is the cause of my woes. I do realise PyQt support/fixes are quite voluntary, and so thank whoever in advance!<br></div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><span style="font-family:tahoma,sans-serif">Kindest,</span></div><div><span style="font-family:tahoma,sans-serif">Jonathan</span></div></div></div></div></div>
</div>