I'm currently looking to display the text of the first column in a treeview in such way that the text takes up the space of both column one and two - sort of like a merged cell in MS Excel. The approach must only apply for item in the tree that have children branches underneath.

I've subclass the QItemDelegate in the following way

Qt Code:
  1. class PostDelegate : public QItemDelegate
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6. PostDelegate(QObject *parent=0) : QItemDelegate(parent){}
  7. ~PostDelegate(){}
  8. void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
  9. {
  10. if(index.model()->hasChildren(index) && index.column() < 2 ) { //GroupProxyRecord
  11. QItemDelegate::drawBackground(painter , option , index) ; //draw bagground first
  12. QModelIndex idx = index;
  13. if(index.column() == 1) { //redirect to column 0
  14. idx = index.sibling(index.row() , 0) ;
  15. }
  16. QSize s = sizeHint(opt , idx) ;
  17. QString value = idx.data().toString() ;
  18. QRectF rectF = painter->boundingRect ( opt.rect , Qt::AlignLeft, value ) ;
  19. painter->drawText(rectF, value );
  20.  
  21. } else {
  22. QItemDelegate::paint(painter , option , index) ;
  23. }
  24.  
  25. }
  26.  
  27. QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
  28. {
  29. return QItemDelegate::sizeHint(option, index);
  30. }
  31.  
  32. };
To copy to clipboard, switch view to plain text mode 

Now, I'm almost there but I can't seem to calculate the rectangle that covers index column one and two in order to calculate the right QRect for the painter.

If you look at the attached picture the merge cell paint must only apply to row 0,1,5 and 9.

My question is: How can I retrieve/calculate the rect associated with a given QModelIndex?

PostDelegate2.png

Any suggestions out there?