Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Ampersand (&) appeared when clicking a button



  • Hello I am using ampersands in QPushbotton for shortcut. They normally don't appear. But when I click a button, they all showed up.
    1.png
    2.png

    During my debugging, I found that it is a ComboBox that triggered this. I want to align text in that combobox to center. So I copied a code from someone. If I disable that code, everything will be alright.

    So what's the problem with this code?

    class AlignDelegate: public QStyledItemDelegate{
    public:
        AlignDelegate(Qt::Alignment alignment, QObject* parent = nullptr):
            QStyledItemDelegate(parent), m_alignment(alignment)
        {
    
        }
    protected:
        void initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const
        {
            QStyledItemDelegate::initStyleOption(option, index);
            option->displayAlignment = m_alignment;
        }
        Qt::Alignment m_alignment;
    };
    
    class AlignComboBoxProxy: public QProxyStyle{
    public:
        using QProxyStyle::QProxyStyle;
        void drawItemText(QPainter* painter, const QRect& rect, int, const QPalette& pal,
                          bool enabled, const QString& text, QPalette::ColorRole textRole) const
        {
            QProxyStyle::drawItemText(painter, rect, Qt::AlignCenter, pal, enabled, text, textRole);
        }
    };
    

    For the combobox, I used

    QComboBox* cb = new QComboBox(this);
    cb->setStyle(new AlignComboBoxProxy(cb->style()));
    cb->addItems(strl);
    cb->setCurrentText(SubList.at(i + i + 1));
    cb->setItemDelegate(new AlignDelegate(Qt::AlignCenter, cb));
    

  • Lifetime Qt Champion

    Hi,

    What version of Qt are you using ?
    On what platform ?


  • Lifetime Qt Champion

    Hi
    I could reproduce it.
    Seems to come from doing
    cb->setStyle(new AlignComboBoxProxy(cb->style()));
    and if i do
    cb->setStyle( new AlignComboBoxProxy() );

    The & is no longer shown. (before you press alt )

    Im not sure what it does with the QStyle given but it does seem then to affect other widgets even you just assign it to the combo.



  • @SGaist said in Ampersand (&) appeared when clicking a button:

    Hi,

    What version of Qt are you using ?
    On what platform ?

    Qt 5.15.0-rc with MSVC 2019 on Win10


  • Lifetime Qt Champion

    Did you check the solution of @mrjj ?



  • @SGaist Yep that's working. I've already reported this.
    https://bugreports.qt.io/browse/QTBUG-84185


  • Lifetime Qt Champion

    Can you add a minimal compilable example to the bug report please ? That will allow the developer to more easily test your issue as well as test the corresponding fix.


Log in to reply