10

Is there a way I could increase the height of the items, which are listed in a QComboBox control ?

I tried following as suggested here in QTDevNet forums but with no luck

QComboBox QAbstractItemView::item {margin-top: 3px;}

I also tried this, still with no result.

QComboBox QAbstractItemView::item {min-height: 20px;}

Is it possible to achieve this at style-sheet level at all ?

warunanc
  • 2,221
  • 1
  • 23
  • 40

3 Answers3

34

Your style sheet seemed correct, so I tried it. It seems the problem is similar to this one on Qt centre:

QCompleter sets a custom QAbstractItemDelegate on its model and unfortunately this custom item delegate does not inherit QStyledItemDelegate but simply QItemDelegate (and then overrides the paint method to show the selected state).

If you replace the default delegate by a QStyledItemDelegate, your style sheet should work:

QStyledItemDelegate* itemDelegate = new QStyledItemDelegate();
combo->setItemDelegate(itemDelegate);

Important: If you change the model, then that will reset the view's delegate, so the above method needs to be called after any call to setModel().

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Leiaz
  • 2,867
  • 2
  • 20
  • 23
  • seems this is the least painful way to implement my requirement, thank you – warunanc Nov 10 '12 at 04:29
  • For me, it still doesn't work. I did what you say, and tried to use the same style, and it doesn't work.Stays in the same way. – Darkgaze Jan 24 '14 at 12:42
  • for anyone like @darkgaze still having an issue after using this solution, try using the `QComboBox ::item` as your selector within the style sheet. This answer along with that selector worked for me. – MildWolfie Nov 22 '17 at 13:11
  • This helped me - thanks and +1! The referenced article says that this must be done after any call to `setModel()` - if true, that's really worth mentioning here. – Toby Speight Nov 23 '18 at 09:57
  • Another suggestion: If the height of items in the view is not working, try to add items from `cpp` after `setItemDelegate`. I ran into a stylesheet problem when adding items from QtDesigner. – Destiny Nov 22 '19 at 09:12
1

An alternative solution would be:

ui->comboBox->model()->setData(ui->comboBox->model()->index(-row-, 0), QSize(-width-, -height-), Qt::SizeHintRole);

, where -row- is zero-based item index; -width- and -height- stand for item width hint and height hint, respectively.

guan boshen
  • 724
  • 7
  • 15
0

QComboBox::item worked for me

So for example, I was trying to change the color of the item when it was disabled, and the following code did the trick.

This one did not work:

QComboBox QAbstractItemView::item:!enabled {
    color:red;
}

Instead I used:

QComboBox::item:!enabled {
    color:red;
}
laurapons
  • 971
  • 13
  • 32