Well, I can tell you how I did it. I have a tree of items that models the structure of the delegate. Items in that tree are subclasses of "DelegateLayoutItem" which can be either subclasses of "DelegateLayout" (which currently can be just DelegateLinearLayout) or DelegateWidget that can be subclassed any way one wants (I have subclasses that offer text or image that comes from the model and static text or static pixmap). The basic idea is that each of them provides a sizeHint() implementation and a paint() implementation. Both of them take a model index and QStyleOptionViewItem instance. The rest is just to calculate sizeHint() down the tree for a specific index and item option and paint everything. Just like for regular widgets or graphics items. I don't respect size policies yet and I only respect right alignment for a vertical layout. There is no event forwarding yet but the same idea can be employed for it.

Here is a sample (not actual, I don't have the code with me right now) implementation of sizeHint:

Qt Code:
  1. QSize DelegateText::sizeHint(const QModelIndex &index, const QStyleOptionViewItem &option) const {
  2. QString text = index.data(m_role).toString();
  3. return option.fontMetrics.boundingRect(option.rect, 0, text).adjusted(-leftMargin(), -topMargin(), rightMargin(), bottomMargin()).size();
  4. }
To copy to clipboard, switch view to plain text mode 

and a sample paint:

Qt Code:
  1. void DelegateText::paint(QPainter *painter, const QModelIndex &index, const QStyleOptionViewItem &option, const QRect &rect) {
  2. QRect r = rect.adjusted(leftMargin(), topMargin(), -rightMargin(), -bottomMargin());
  3. painter->drawText(r, 0, index.data(m_role).toString());
  4. }
To copy to clipboard, switch view to plain text mode