Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Ampersand (&) appeared when clicking a button
Qt 6.11 is out! See what's new in the release blog

Ampersand (&) appeared when clicking a button

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MasterBlade
    wrote on last edited by
    #1

    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));
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

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

          M Offline
          M Offline
          MasterBlade
          wrote on last edited by
          #4

          @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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Did you check the solution of @mrjj ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            M 1 Reply Last reply
            1
            • SGaistS SGaist

              Did you check the solution of @mrjj ?

              M Offline
              M Offline
              MasterBlade
              wrote on last edited by
              #6

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

              1 Reply Last reply
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved