<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 17 May 2018 at 11:18, Florian Bruhin <span dir="ltr"><<a href="mailto:me@the-compiler.org" target="_blank">me@the-compiler.org</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 Thu, May 17, 2018 at 11:14:18AM +0100, J Barchan wrote:<br>
> On 17 May 2018 at 10:12, Florian Bruhin <<a href="mailto:me@the-compiler.org">me@the-compiler.org</a>> wrote:<br>
> <br>
> > On Thu, May 17, 2018 at 09:50:13AM +0100, J Barchan wrote:<br>
> > > I'm not sure whether I am hijacking a relevant thread, or whether this<br>
> > > can already be done, but I would like to know how to (easily) get the<br>
> > > symbolic string for an ItemDataRole constant:<br>
> > ><br>
> > > class MySqlQueryModel(<wbr>QSqlQueryModel):<br>
> > > def data(self, index: QtCore.QModelIndex, role:<br>
> > ><br>
> > <br>
> > QtCore.Qt.ItemDataRole=QtCore.<wbr>Qt.DisplayRole) -> typing.Any:<br>
> > > print(role)<br>
> > ><br>
> > > How do I get to print that role as, say, "DisplayRole" instead of as 0,<br>
> > > for debugging?<br>
> ><br>
> > I wrote something to do that here:<br>
> > <a href="https://github.com/qutebrowser/qutebrowser/blob/v1.3.0/qutebrowser/utils/" rel="noreferrer" target="_blank">https://github.com/<wbr>qutebrowser/qutebrowser/blob/<wbr>v1.3.0/qutebrowser/utils/</a><br>
> > debug.py#L96-L132<br>
> ><br>
> > Florian<br>
> ><br>
> > --<br>
> > <a href="https://www.qutebrowser.org" rel="noreferrer" target="_blank">https://www.qutebrowser.org</a> | <a href="mailto:me@the-compiler.org">me@the-compiler.org</a> (Mail/XMPP)<br>
> > GPG: 916E B0C8 FD55 A072 | <a href="https://the-compiler.org/pubkey.asc" rel="noreferrer" target="_blank">https://the-compiler.org/<wbr>pubkey.asc</a><br>
> > I love long mails! | <a href="https://email.is-not-s.ms/" rel="noreferrer" target="_blank">https://email.is-not-s.ms/</a><br>
> ><br>
> <br>
> Dear Florian,<br>
> <br>
</span>> Thanks! Your qenum_key() looks promising *except for*:<br>
> <br>
> <br>
> 1. I cannot find anything suitable to pass in as `base`, e.g.<br>
<span class="gmail-">> <br>
> qenum_key(QtCore.Qt, role, <div style="font-family:tahoma,sans-serif;display:inline" class="gmail_default"></div>klass=QtCore.Qt.ItemDataRole)<br>
> <br>
> does not work. I don't understand what it is I need here which has a<br>
> base.staticMetaObject?<br>
</span>> 2. Is this whole principle going to work for QtCore.Qt.ItemDataRole? So<br>
<span class="gmail-">> far as I can see, it's not some kind of enum in PyQt, it's just a class<br>
> derived from int with a bunch of sundry member variables for various<br>
> values.<br>
<br>
</span>Seems to work fine for me:<br>
<br>
>>> from qutebrowser.utils import debug<br>
>>> from PyQt5.QtCore import Qt<br>
>>> debug.qenum_key(Qt, Qt.DisplayRole)<br>
'DisplayRole'<br>
<br>
(tried with PyQt 5.7.1 and 5.10)<br>
<div class="gmail-HOEnZb"><div class="gmail-h5"><br>
Florian<br>
<br>
-- <br>
<a href="https://www.qutebrowser.org" rel="noreferrer" target="_blank">https://www.qutebrowser.org</a> | <a href="mailto:me@the-compiler.org">me@the-compiler.org</a> (Mail/XMPP)<br>
GPG: 916E B0C8 FD55 A072 | <a href="https://the-compiler.org/pubkey.asc" rel="noreferrer" target="_blank">https://the-compiler.org/<wbr>pubkey.asc</a><br>
I love long mails! | <a href="https://email.is-not-s.ms/" rel="noreferrer" target="_blank">https://email.is-not-s.ms/</a><br>
</div></div></blockquote></div><br><div style="font-family:tahoma,sans-serif" class="gmail_default">My humble apologies!</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">I was confused in the PyCharm IDE, which highlights passing <span style="font-family:monospace,monospace">QtCore.Qt</span> for <span style="font-family:monospace,monospace">base</span> with error message "Type '[Qt]' doesn't have expected attribute 'staticMetaObject'", and then stepping through qenum_key hit that <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">except </span><span style="color:rgb(0,0,128)">AttributeError</span>:</pre> code path, and I thought it had gone wrong. I now understand how your code works, and that path is expected in my case!</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">For my case, I want to know the symbolic value in a <span style="font-family:monospace,monospace">role</span> <i>variable</i>, which has already been converted to an <span style="font-family:monospace,monospace">int</span>. That means I must pass <span style="font-family:monospace,monospace">qenum_key()</span> a <span class="gmail-"><div style="font-family:tahoma,sans-serif;display:inline" class="gmail_default"></div><span style="font-family:monospace,monospace">klass=QtCore.Qt.ItemDataRole</span></span> hint. I now confirm my</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">qenum_key(QtCore.Qt, role, <span style="color:rgb(102,0,153)">klass</span>=QtCore.Qt.ItemDataRole)</pre>is the correct formulation for me.<br></div></div><div class="gmail_extra"><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">I shall now add your <span style="font-family:monospace,monospace">qenum_key()</span> & <span style="font-family:monospace,monospace">qflags_key()</span> to my utility functions! Thanks :)<br></div><br clear="all"><br>-- <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>