[PyQt] Getting parent class for an enum
J Barchan
jnbarchan at gmail.com
Thu May 17 11:44:31 BST 2018
On 17 May 2018 at 11:18, Florian Bruhin <me at the-compiler.org> wrote:
> On Thu, May 17, 2018 at 11:14:18AM +0100, J Barchan wrote:
> > On 17 May 2018 at 10:12, Florian Bruhin <me at the-compiler.org> wrote:
> >
> > > On Thu, May 17, 2018 at 09:50:13AM +0100, J Barchan wrote:
> > > > I'm not sure whether I am hijacking a relevant thread, or whether
> this
> > > > can already be done, but I would like to know how to (easily) get the
> > > > symbolic string for an ItemDataRole constant:
> > > >
> > > > class MySqlQueryModel(QSqlQueryModel):
> > > > def data(self, index: QtCore.QModelIndex, role:
> > > >
> > >
> > > QtCore.Qt.ItemDataRole=QtCore.Qt.DisplayRole) -> typing.Any:
> > > > print(role)
> > > >
> > > > How do I get to print that role as, say, "DisplayRole" instead of
> as 0,
> > > > for debugging?
> > >
> > > I wrote something to do that here:
> > > https://github.com/qutebrowser/qutebrowser/blob/
> v1.3.0/qutebrowser/utils/
> > > debug.py#L96-L132
> > >
> > > Florian
> > >
> > > --
> > > https://www.qutebrowser.org | me at the-compiler.org (Mail/XMPP)
> > > GPG: 916E B0C8 FD55 A072 | https://the-compiler.org/pubkey.asc
> > > I love long mails! | https://email.is-not-s.ms/
> > >
> >
> > Dear Florian,
> >
> > Thanks! Your qenum_key() looks promising *except for*:
> >
> >
> > 1. I cannot find anything suitable to pass in as `base`, e.g.
> >
> > qenum_key(QtCore.Qt, role,
>
> klass=QtCore.Qt.ItemDataRole)
> >
> > does not work. I don't understand what it is I need here which has a
> > base.staticMetaObject?
> > 2. Is this whole principle going to work for QtCore.Qt.ItemDataRole?
> So
> > far as I can see, it's not some kind of enum in PyQt, it's just a
> class
> > derived from int with a bunch of sundry member variables for various
> > values.
>
> Seems to work fine for me:
>
> >>> from qutebrowser.utils import debug
> >>> from PyQt5.QtCore import Qt
> >>> debug.qenum_key(Qt, Qt.DisplayRole)
> 'DisplayRole'
>
> (tried with PyQt 5.7.1 and 5.10)
>
> Florian
>
> --
> https://www.qutebrowser.org | me at the-compiler.org (Mail/XMPP)
> GPG: 916E B0C8 FD55 A072 | https://the-compiler.org/pubkey.asc
> I love long mails! | https://email.is-not-s.ms/
>
My humble apologies!
I was confused in the PyCharm IDE, which highlights passing QtCore.Qt for
base with error message "Type '[Qt]' doesn't have expected attribute
'staticMetaObject'", and then stepping through qenum_key hit that
except AttributeError:
code path, and I thought it had gone wrong. I now understand how your code
works, and that path is expected in my case!
For my case, I want to know the symbolic value in a role *variable*, which
has already been converted to an int. That means I must pass qenum_key() a
klass=QtCore.Qt.ItemDataRole hint. I now confirm my
qenum_key(QtCore.Qt, role, klass=QtCore.Qt.ItemDataRole)
is the correct formulation for me.
I shall now add your qenum_key() & qflags_key() to my utility functions!
Thanks :)
--
Kindest,
Jonathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180517/5fd07785/attachment-0001.html>
More information about the PyQt
mailing list