<div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif"></div><div class="gmail_extra"><br></div><div class="gmail_quote">On 8 May 2018 at 18:14, 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;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid"><span>On 8 May 2018, at 9:04 am, J Barchan <<a href="mailto:jnbarchan@gmail.com">jnbarchan@gmail.com</a>> wrote:<br>
> <br>
> Now I'm finding that, with the fix discussed, while my overridden function definition correctly handles database NULLs, it "goes wrong" (as in, different behaviour from before) in certain other cases, returning a QVariant where it did not do so before (it returned the converted, native Python type).<br>
> <br>
> 1. So long as I do not override QSqlQueryModel.data() at all, there is absolutely no problem --- both database NULL and auto-conversion of non-NULL to Python native type work fine, and are distinct. This is the situation I need.<br>
> <br>
> 2. I need to override QSqlQueryModel.data() for my own purposes. If I write just:<br>
> def data(self, index: QtCore.QModelIndex, role=QtCore.Qt.DisplayRole) -> typing.Any:<br>
> value = super().data(index, role)<br>
> return value<br>
> Some data conversion happens, such that I no longer get NULL back where the value is NULL --- instead it is converted to '' if string or 0 if int. This was my original problem and is not acceptable.<br>
> <br>
> 3. Following our discussion, I change that to:<br>
> def data(self, index: QtCore.QModelIndex, role=QtCore.Qt.DisplayRole) -> typing.Any:<br>
> was_enabled = sip.enableautoconversion(<wbr>QtCore.QVariant, False)<br>
> value = super().data(index, role)<br>
> sip.enableautoconversion(<wbr>QtCore.QVariant, was_enabled)<br>
> return value<br>
> Now I correctly get whatever for database NULL, which works. However, some other path of code, on some quite different non-NULL value, gets back a QVariant where it used to get a string. I don't know what that path of code is, but I don't think I should care.<br>
> <br>
> So, what I need is: code which allows me to override <div class="gmail_default" style="font-family:tahoma,sans-serif;display:inline"></div>QSqlQueryModel.data() but returns the original data() value unchanged, just like it used when I did not put any override in (case #1). It must do whatever to correctly deal with NULL & non-NULL, just like the non-overridden QSqlQueryModel.data() does.<br>
> <br>
> (In PyQt 5.7) What exact code can I put into the override to achieve just that, please? Surely that can be done, no?<br>
<br>
</span>You can't have it both ways. Either you let PyQt automatically convert to/from QVariant (and you lose the detection of nulls) or you do it yourself (converting to Python using the value() method).<br>
<br>
By the way, I've just noticed a bug in the docs which says (incorrectly) that null QVariants are converted to None and vice versa.<br>
<span class="gmail-HOEnZb"><font color="#888888"><br>
Phil</font></span></blockquote></div><div class="gmail_extra"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">Hi Phil,</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">Thanks for your reply.</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">I think one of us <em>must</em> be getting something wrong here. I wonder if you're still expecting me to understand something which is obvious to you but not to me.</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid">You can't have it both ways. Either you let PyQt automatically convert to/from QVariant (and you lose the detection of nulls) or you do it yourself (converting to Python using the value() method).</blockquote><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">I'm not asking to have anything both ways. I'm just asking how to write code so that the overridden method behaves <em>absolutely identically</em> to the base method it's overriding. Surely that must be possible?</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">I remind you: when I have no override for <font face="monospace,monospace">QSqlQueryModel.data()</font> <em>everything behaves perfectly</em>. I am saying: there is no problem, NULLs are handled as such and non-NULLs are correctly converted to their Python equivalent. I do not know how NULLs work (what they are returned as), but everything just works.</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">As soon as I write an override which just returns the base method, it goes wrong on <font face="monospace,monospace">NULL</font>. If I put it the <font face="monospace,monospace">sip.autoconversion(False)</font>, it works on <font face="monospace,monospace">NULL</font> but now returns a <font face="monospace,monospace">QVariant </font>where it used to return a Python native type, I think.</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">All I want to know is: how can I write an override of <font face="monospace,monospace">QSqlQueryModel.data()</font> in Python/PyQt like:</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif"><font color="#500050" face="monospace,monospace">def data(self, index: QtCore.QModelIndex, role=QtCore.Qt.DisplayRole) -> typing.Any:<br> value = super().data(index, role)<br> return value</font></div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">such that it returns <em>just exactly the same as</em> <font face="monospace,monospace">QSqlQueryModel.data() </font>would have done, please, please, please?</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br>-- <br></div><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 class="gmail_extra">
</div></div>