PDA

View Full Version : Delegate and Stylesheet



curreli
29th July 2010, 16:54
Hi,

I have a stylesheet for a QListView which is linked to a QStyledItemDelegate in order to paint item in a custom way.

I have a problem when adding some style for the items:

String style = "QListView {background: url(" + GlobalConstants.IMAGE_PATH + "background.png); }";
style += " QListView::item { color: white; }";
style += " QListView::item:hover { color: red; }";

The QListView style works fine but the QListView::item style does not appear.
I've tried to unlink the QStyledItemDelegate and the item style did work!

So my question is, how can we set style for the list items when using a delegate?

Thanks.

aamer4yu
29th July 2010, 17:46
How are you painting in your custom item delegate ?

I dont think you are using QStyle functions to draw...

GreenScape
29th July 2010, 17:53
here one technique using QLabel as content renderer



void SomeItemDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QLabel renderer;
renderer.setStyleSheet(getStyleSheet(index));
renderer.setText(makeDisplayString(index));
renderer.resize(option.rect.size());
painter->save();
painter->translate(option.rect.topLeft());
renderer.render(painter);
painter->restore();
}

curreli
30th July 2010, 13:51
Thanks, this helped me a lot!