<div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_extra"><br><div class="gmail_quote">On 3 May 2018 at 16:17, Phil Thompson <span dir="ltr"><<a href="mailto:phil@riverbankcomputing.com" target="_blank">phil@riverbankcomputing.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="gmail-">On 3 May 2018, at 3:22 pm, J Barchan <<a href="mailto:jnbarchan@gmail.com">jnbarchan@gmail.com</a>> wrote:<br>
> Hey Phil,<br>
> <br>
> Assuming I am not barking up the wrong tree, I see you yourself were discussing this issue in <a href="http://python.6.x6.nabble.com/PyQt5-NULL-QVariant-tp5188782p5190811.html" rel="noreferrer" target="_blank">http://python.6.x6.nabble.com/<wbr>PyQt5-NULL-QVariant-<wbr>tp5188782p5190811.html</a>.  It includes:<br>
> <br>
> > Can you confirm that returning empty string and 0 for NULL is expected <br>
> > behavior and not seen as a bug? <br>
> <br>
> Yes. If you want to distinguish null QVariants then use sip.enableautoconversion(). The problem is that you are not willing to do that. <br>
> <br>
> So I'm bumbling around trying to put a sip.enableautoconversion() in!  Trouble is, I don't understand the syntax, and I don't know where to put it (I'm not finding any examples, and you two obviously knew more than me about it :) )<br>
> <br>
> 1. I'm assuming I want to suppress converting QVariants.  I'm trying <br>
> <div style="font-family:tahoma,sans-serif;display:inline" class="gmail_default">​​</div>sip.enableautoconversion(<wbr>QVariant, False)<br>
> but it doesn't know what QVariant is, and I don't know if it's supposed to or my syntax or imports or whatever?<br>
<br>
</span><div style="font-family:tahoma,sans-serif;display:inline" class="gmail_default">​​</div>Import QVariant from QtCore.<br>
<span class="gmail-"><br>
> 2. I don't know where I'm supposed to disable & re-enable the autoconversion?  My method is an override called implicitly by Qt.<br>
> * Is this a "directive" that's supposed to be used e.g. as a Python method is read in, and so belongs around the whole function?<br>
> * Is this a "run-time* that I just need to put inside my method overload around where it calls the base class method?  Or by the time it has hit my overload is it too late because some conversion has already happened?<br>
> * Do I actually need to <div style="font-family:tahoma,sans-serif;display:inline" class="gmail_default">​​</div>put it around the whole of my top-level call to, say, QSqlQueryModel.rowCount(), which is what (I understand to be) invoking calls to the overridden QSqlQueryModel.data() method?<br>
<br>
</span><div style="font-family:tahoma,sans-serif;display:inline" class="gmail_default">​​</div>I think you need to do the latter - at least as a workaround for the moment. I <div style="font-family:tahoma,sans-serif;display:inline" class="gmail_default">​​</div>need to add a directive to SIP to disable the auto-conversion during the code that handles the virtual and calls the Python re-implementation.<br>
<span class="gmail-HOEnZb"><font color="#888888"><br>
Phil</font></span></blockquote></div><br><div style="font-family:tahoma,sans-serif" class="gmail_default">​Hi Phil,</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">Right, I think we're in business, here goes...!​</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">First, I took your *original* response of "No and no." to indicate that my issue would have nothing to do with PyQt.  In the end I think we are agreeing it has turned out to be a PyQt issue after all.</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">Second, yes, I figured "<div style="font-family:tahoma,sans-serif;display:inline" class="gmail_default">​</div>Import QVariant from QtCore" was what I was missing.  (Not helped by PyCharm warning me it really wanted a "sip wrappertype" for <span style="font-family:monospace,monospace">sip.enableautoconversion()</span> instead of a "type", but it works OK at run-time.)  I actually interpreted <a href="http://pyqt.sourceforge.net/Docs/PyQt5/pyqt_qvariant.html">http://pyqt.sourceforge.net/Docs/PyQt5/pyqt_qvariant.html</a>:</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="font-family:tahoma,sans-serif" class="gmail_default">v2 (the default for
Python v3) does not expose the <a class="gmail-reference external" href="http://pyqt.sourceforge.net/Docs/PyQt5/api/QtCore/qvariant.html#PyQt5-QtCore-QVariant"><code class="gmail-xref gmail-sip gmail-sip-class gmail-docutils gmail-literal gmail-notranslate"><span class="gmail-pre">QVariant</span></code></a> class to
Python<br></div></blockquote><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">​as meaning that <span style="font-family:monospace,monospace">QVariant</span> was not going to be defined/exported in any PyQt file​ at all.  But I see it still is.</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">Third, in view of your "<div style="font-family:tahoma,sans-serif;display:inline" class="gmail_default">​</div>I think you need to do the latter [<span class="gmail-"><div style="font-family:tahoma,sans-serif;display:inline" class="gmail_default">​</div>put <span class="gmail-"><div style="font-family:tahoma,sans-serif;display:inline" class="gmail_default">​</div><span style="font-family:monospace,monospace">sip.enableautoconversion</span></span><span style="font-family:monospace,monospace">()</span> around the whole of my top-level call</span>] - at least as a workaround for the moment.", I started out doing that.  However, I found that the <span style="font-family:monospace,monospace">data()</span> method is being called all over the place (e.g. throughout the <span style="font-family:monospace,monospace">QTableView</span> displaying the model), which I guess makes sense, so that was soon a non-starter.  In any case it would be way-dangerous to have to change <span style="font-family:monospace,monospace">QVariant</span> behaviour across a large area of code.  I would hugely prefer to only have to do so across the call to the base method from the derived method, and to my pleasure it <i>seems</i> to be working just fine like that.  So my override (to give you a flavor of why I need it) now actually reads:</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default"><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>    DBModelCommon.dataIndexIsValid(index)    # do some checking that index is valid<br><br>    <span style="color:rgb(0,0,128);font-weight:bold">import </span>sip<br>    sip.enableautoconversion(QtCore.QVariant, <span style="color:rgb(0,0,128);font-weight:bold">False</span>)<br>    value = <span style="color:rgb(0,0,128)">super</span>().data(index, role)        # wrap call to QSqlQueryModel.data() inside no-convert-QVariant-to-Python<br>    sip.enableautoconversion(QtCore.QVariant, <span style="color:rgb(0,0,128);font-weight:bold">True</span>)<br><br>    <span style="color:rgb(0,0,128);font-weight:bold">return </span>DBModelCommon.dataValue(value, role)    # do some (potential) post-processing on the value returned</pre><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">​You will understand this better than I, but the implication is that the problem is not in my own Python definition of this override itself already doing a conversion and going wrong, but rather at the time it calls the base function and grabs its return value into Python?<br></div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">So far all I can say is that from what i have seen it now handles my values --- both NULLs and non-NULLs --- ​correctly! :)</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">Finally, I'm interested to understand what is going on here, and why you "<div style="font-family:tahoma,sans-serif;display:inline" class="gmail_default">​</div>need
 to add a directive to SIP to disable the auto-conversion during the 
code that handles the virtual and calls the Python re-implementation".  I'm<i> assuming</i> the override was returning a <span style="font-family:monospace,monospace">QVariant</span>-converted-to-Python-type, then something about other (Qt) C++ code calling the override and not being happy about the return type, or something?  So how do you (or I) know whether there are going to be other cases (overrides) which, say, return or pass or receive a <span style="font-family:monospace,monospace">QVariant</span> (which could be a NULL one), and will also fall foul of this issue?  Do you/I need to look through every Qt function?  Have you already done this and are confident that there can be only a few remaining which you have not spotted?  Is that how it works?</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">Thanks for all your attention!!</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br clear="all"></div></div><div class="gmail_extra">-- <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></div>