[PyQt] Getting parent class for an enum

Florian Bruhin me at the-compiler.org
Thu May 17 11:18:29 BST 2018


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/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20180517/95e13eb6/attachment.sig>


More information about the PyQt mailing list