I have implemented my own delegate for a qTreeWidget, it works perfectly (for what I am using it for at least the widget contains about 30 items and the template affects only top level items) however I think the solution is a bit ugly and I would appreciate some advice.

The technique I use is to create a template widget (which I have created in the designer), assign the data to it, then render it to a pixmap and paint the pixmap.
The ugly part is how I am setting the style on the template widget, ideally I would simply like to apply the appropriate "QTreeView::item" style from my .qss file but aside from string parsing them out of the .qss file manually I don't know how to do that, and all my attempts to use the qstyleoptionview object failed.

Qt Code:
  1. class ItemTemplate: public QWidget
  2. {
  3. public:
  4. ItemTemplate(QWidget* parent = 0);
  5. void setDescription(const QString& s);
  6.  
  7. private:
  8. Ui::ItemTemplateWidget ui;
  9. };
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. class CustomItemDelegate : public QStyledItemDelegate
  2. {
  3. public:
  4. ...
  5. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
  6.  
  7.  
  8. private:
  9. ItemTemplate* mTemplateWidget;
  10. };
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. void CustomItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  2. {
  3. QString s = index.data(ROLE_DESCRIPTION).toString();
  4. if (!s.isEmpty() && !s.isNull())
  5. {
  6. painter->save();
  7.  
  8. QStyleOptionViewItemV4 opt = option;
  9. initStyleOption(&opt, index);
  10.  
  11. if (option.state & QStyle::State_Enabled)
  12. {
  13. mTemplateWidget->setStyleSheet("somecustomstyle");
  14.  
  15. if (option.state & QStyle::State_Selected)
  16. {
  17. mTemplateWidget->setStyleSheet("somecustomstyle");
  18. }
  19. else if (option.state & QStyle::State_MouseOver)
  20. {
  21. mTemplateWidget->setStyleSheet("somecustomstyle");
  22. }
  23. }
  24. else
  25. {
  26. mTemplateWidget->setStyleSheet("somecustomstyle");
  27. }
  28.  
  29. mTemplateWidget->setDescription(s);
  30. mTemplateWidget->setGeometry(0, 0, option.rect.width(), option.rect.height());
  31.  
  32. QPixmap pix = QPixmap::grabWidget(mTemplateWidget);
  33. painter->drawPixmap(option.rect, pix);
  34.  
  35. painter->restore();
  36. }
  37. else
  38. {
  39. QStyledItemDelegate::paint(painter, option, index);
  40. }
To copy to clipboard, switch view to plain text mode 

Thanks in advance!