[PyQt] QPushButton.text() ampersand issue

Florian Bruhin me at the-compiler.org
Sat Oct 10 11:59:21 BST 2015


* Samuele Carcagno <sam.carcagno at gmail.com> [2015-10-09 18:42:47 +0100]:
> I'm having an issue getting the text from a QPushButton.The text of the
> button is set with:
> 
>     statusButton = QPushButton(translator.translate('rb', "Start"), self)
> 
> the text appears normally on the button (i.e. without an ampersand), but
> when I get the text with:
> 
>     status = statusButton.text()
> 
> the string returned is "&Start" instead of "Start". Translated strings are
> also returned with an ampersand (e.g. "&Inizia" instead of "Inizia"). This
> issue is occurring with PyQt 5.4.1 on Kubuntu Vivid, and with PyQt 5.4.2 on
> Debian testing, but not on Debian Jessie with PyQt 5.3.2.

The ampersand shows the shortcut character for the QPushButton, see
the documentation[1].

I'm guessing the behaviour was changed in Qt for 5.4 in some way, not
sure if accidentally or not.

If you're sure your texts don't actually contain ampersands, you could
just do .replace('&', '') on the string you get. Or you could store
the string somewhere yourself.

Florian

[1] http://doc.qt.io/qt-5/qpushbutton.html#details

-- 
http://www.the-compiler.org | me at the-compiler.org (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072 | http://the-compiler.org/pubkey.asc
         I love long mails! | http://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20151010/a0e54a90/attachment.sig>


More information about the PyQt mailing list