Search code examples
c++qtlistviewqt5qstyleditemdelegate

How to paint according to qStylesheet in QStyledItemDelegate


I want to subclass QStyledItemDelegate and modify it with QStyleSheets. I dont have any clue of how to get it to work. I tried plenty of hours to just display anything, looking like I defined in the qss. In priciple it has to work, because when I call the base class implementation of paint(QPainter*, const QStyleOptionViewItem &, const QModelIndex&) the list items look like defined. Well, now how can I apply the stylesheet in the delegate?


Solution

  • Everything has to be done through QStyle. E.g. in a QListView to draw the background:

    QStyle *style = option.widget->style();
    style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, option.widget);
    

    There are a lot of other drawXYZ(..) functions. See QStyle docs.