3

I want to style my QComboBox with margin for the drop down items. This is how it looks like now:

enter image description here

And I would like something like this:

enter image description here

I tried with

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

but it doesn't work.

Can you help me with this?

Jacob Krieg
  • 2,834
  • 15
  • 68
  • 140

2 Answers2

12

You want set some space between items.I try different stylesheets, but I can't get result. But I found this solution. Maybe it helps.

    ui->comboBox->setView(new QListView());
ui->comboBox->setStyleSheet("QComboBox QAbstractItemView::item { min-height: 35px; min-width: 50px; }QListView::item:selected { color: black; background-color: lightgray}");

Result is very similar enter image description here

Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • This is very strange because I'm not getting what you get. I just created an empty project, dragged a QComboBox inside and pasted your code but it does not work. This is what I have: test.pro(http://pastebin.com/fihj8944), mainwindow.h(http://pastebin.com/qMiazMVd), mainwindow.cpp(http://pastebin.com/FnHdteZU), main.cpp(http://pastebin.com/fsDD1kFr) and mainwindow.ui(http://pastebin.com/NCjMsj43). What am I doing wrong? – Jacob Krieg Aug 16 '14 at 17:45
  • @JacobKrieg , I carefully watch your code, but I don't see in your code ui->comboBox->setView(new QListView()); Add this line before setStyleSheet() . First ui->comboBox->setView(new QListView()); and after - setStyleSheet() (I hope you understand me). And after adding of this line it should work. – Jablonski Aug 16 '14 at 17:57
  • But I added the elements using the designer. Shouldn't this work? – Jacob Krieg Aug 17 '14 at 13:29
  • In my designer I added it too, but without this line it doesn't works. Just add this line and you'll see that this work properly(I'm sure,because without this line my app don't want add this space)Moreover,with this line,it works properly when I add icons in the combobox. I think, that it is a solution,try this – Jablonski Aug 17 '14 at 14:03
  • You are right, indeed it worked. Thank you very much! :) – Jacob Krieg Aug 17 '14 at 15:06
  • @Chernobyl, you're a genius! – santahopar Sep 26 '18 at 06:55
2
ui->comboBox->setItemDelegate(new QStyledItemDelegate());

also works

hoozh
  • 43
  • 6