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. QComboBox with icons but no text - how?
Forum Update on Monday, May 27th 2025

QComboBox with icons but no text - how?

Scheduled Pinned Locked Moved Solved General and Desktop
qcomboboxqiconalign
6 Posts 3 Posters 6.2k Views
  • 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.
  • W Offline
    W Offline
    wavelike
    wrote on 12 Dec 2015, 10:25 last edited by
    #1

    I want to have QComboBox populated with QIcon items, nothing else, and I would like them to be centered. Problem is, the constructor asks for QString too, which is placed to the right of the corresponding QIcon.

    void QComboBox::addItem(const QIcon & icon, const QString & text, const QVariant & userData = QVariant())
    

    Even if I use an empty QString, I don't see a way to center the icon as only text alignment role is what I see in documentation. QIcon always stays on the left.

    comboBox->setItemData(i, Qt::AlignCenter, Qt::TextAlignmentRole);
    

    There must be a way to do this but I can't find it. Any help?
    Thanks.

    K 1 Reply Last reply 12 Dec 2015, 11:14
    0
    • W wavelike
      12 Dec 2015, 10:25

      I want to have QComboBox populated with QIcon items, nothing else, and I would like them to be centered. Problem is, the constructor asks for QString too, which is placed to the right of the corresponding QIcon.

      void QComboBox::addItem(const QIcon & icon, const QString & text, const QVariant & userData = QVariant())
      

      Even if I use an empty QString, I don't see a way to center the icon as only text alignment role is what I see in documentation. QIcon always stays on the left.

      comboBox->setItemData(i, Qt::AlignCenter, Qt::TextAlignmentRole);
      

      There must be a way to do this but I can't find it. Any help?
      Thanks.

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 12 Dec 2015, 11:14 last edited by
      #2

      @wavelike
      Hello, I'm not aware of any way besides setting an item delegate. You may want to try that.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • W Offline
        W Offline
        wavelike
        wrote on 12 Dec 2015, 11:19 last edited by
        #3

        Hi kshegunov,

        That crossed my mind, but I was hoping there would be a simple way to do it as what I am asking for seems very standard.
        I guess setting up an item delegate is what I should do then.

        Thanks.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 12 Dec 2015, 15:10 last edited by Chris Kawa 12 Dec 2015, 15:11
          #4

          @wavelike said:

          what I am asking for seems very standard

          I guess that's debatable. I don't think I ever saw any app doing that and I would not call it standard.
          Anyway, a delegate is the right thing to do here e.g.

          struct Delegate: public QStyledItemDelegate {
              Delegate(QObject* parent = nullptr) : QStyledItemDelegate(parent) {}
              void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                  auto o = option;
                  initStyleOption(&o, index);
                  o.decorationSize.setWidth(o.rect.width());
                  auto style =  o.widget ? o.widget->style() : QApplication::style();
                  style->drawControl(QStyle::CE_ItemViewItem, &o, painter, o.widget);
              }
          };
          
          W 1 Reply Last reply 12 Dec 2015, 17:10
          1
          • C Chris Kawa
            12 Dec 2015, 15:10

            @wavelike said:

            what I am asking for seems very standard

            I guess that's debatable. I don't think I ever saw any app doing that and I would not call it standard.
            Anyway, a delegate is the right thing to do here e.g.

            struct Delegate: public QStyledItemDelegate {
                Delegate(QObject* parent = nullptr) : QStyledItemDelegate(parent) {}
                void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                    auto o = option;
                    initStyleOption(&o, index);
                    o.decorationSize.setWidth(o.rect.width());
                    auto style =  o.widget ? o.widget->style() : QApplication::style();
                    style->drawControl(QStyle::CE_ItemViewItem, &o, painter, o.widget);
                }
            };
            
            W Offline
            W Offline
            wavelike
            wrote on 12 Dec 2015, 17:10 last edited by
            #5

            @Chris-Kawa

            Thanks Chris, a lot, that is very helpful.

            I was thinking of e.g. drawing applications - don't they use QComboBox-like widgets to offer different colors, brush thickness etc? Those never have any text in them. I might be wrong though as I rarely use them.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on 13 Dec 2015, 18:14 last edited by
              #6

              @wavelike said:

              don't they use QComboBox-like widgets to offer different colors, brush thickness etc?

              Hm, I guess that's true, but I'd say it has more to do with QMenus and QToolButtons than QComboBox. But then again they are indeed similar in some ways. Oh well, the important thing is it's doable and quite easy ;)

              1 Reply Last reply
              0

              5/6

              12 Dec 2015, 17:10

              • Login

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