PDA

View Full Version : Styling a QAbstractItemView item



Berryblue031
2nd March 2011, 13:58
I am attempting to style the items in QCompleter's dropdown via .qss

In my .qss file styles I declare in


QAbstractItemView {

}

work however styles in


QAbstractItemView::item {

}

are completely ignored. In particular I am trying to increase spacing between items, does anybody know how to do that?

Berryblue031
10th March 2011, 13:49
Ok I came back to this problem and found the problem & solution! So I thought I would share because I came across other people posting similar problems but never anybody posting a solution.

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

So in order to use the stylesheet ::item subcontrol with QCompleter you need to do something like this



mCompleterItemDelegate = new QStyledItemDelegate(this);

...

mCompleter.setModel(someitemmodel);
mCompleter.popup()->setItemDelegate(mCompleterItemDelegate); //Must be set after every time the model is set


Now the items in the QCompleter popup list can be styled via standard stylesheet syntax


QListView::item {
margin: 2px;
}

QListView::item:selected {
background: orange;
}

Kamandol
24th March 2011, 09:25
Many thanks for sharing the solution!

It's a shame that isn't possible at the moment to make it work from Designer's .ui forms. Having to set the StyledItemDelegate for every QComboBox(in my case) right after calling the form setupUi() method is quite painful...but better than nothing :).