[PyQt] QPushButton.text() ampersand issue

Samuele Carcagno sam.carcagno at gmail.com
Sun Oct 11 21:19:23 BST 2015


On 10/10/15 11:59, Florian Bruhin wrote:
> * 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.

Thanks, I've already deployed a similar workaround. I'm just curious to 
know whether this is an intentional change or a bug. I thought that the 
issue may be related to a button shortcut, this is the exact snippet of 
code I'm using:

         self.statusButton = 
QPushButton(self.prm['rbTrans'].translate('rb', "Wait"), self)
         self.statusButton.clicked.connect(self.onClickStatusButton)
 
self.statusButton.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, 
QSizePolicy.Expanding))
         self.statusButton.setProperty("responseBoxButton", True)
         self.statBtnShortcut = QShortcut("Ctrl+R", self, activated = 
self.onClickStatusButton)
         self.statusButton.setToolTip(self.tr("Press Ctrl+R to activate"))

but even if I comment the line where the `QShortcut` is set the issue 
persists.

Cheers,

Sam

>
> Florian
>
> [1] http://doc.qt.io/qt-5/qpushbutton.html#details
>
>
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>



More information about the PyQt mailing list