Nightfox
3rd October 2010, 22:09
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
class PostDelegate : public QItemDelegate
{
Q_OBJECT
public:
PostDelegate(QObject *parent=0) : QItemDelegate(parent){}
~PostDelegate(){}
void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
if(index.model()->hasChildren(index) && index.column() < 2 ) { //GroupProxyRecord
QItemDelegate::drawBackground(painter , option , index) ; //draw bagground first
QModelIndex idx = index;
if(index.column() == 1) { //redirect to column 0
idx = index.sibling(index.row() , 0) ;
}
QStyleOptionViewItem opt ;
QSize s = sizeHint(opt , idx) ;
QString value = idx.data().toString() ;
QRectF rectF = painter->boundingRect ( opt.rect , Qt::AlignLeft, value ) ;
painter->drawText(rectF, value );
} else {
QItemDelegate::paint(painter , option , index) ;
}
}
QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
{
return QItemDelegate::sizeHint(option, index);
}
};
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?
5267
Any suggestions out there?
I've subclass the QItemDelegate in the following way
class PostDelegate : public QItemDelegate
{
Q_OBJECT
public:
PostDelegate(QObject *parent=0) : QItemDelegate(parent){}
~PostDelegate(){}
void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
if(index.model()->hasChildren(index) && index.column() < 2 ) { //GroupProxyRecord
QItemDelegate::drawBackground(painter , option , index) ; //draw bagground first
QModelIndex idx = index;
if(index.column() == 1) { //redirect to column 0
idx = index.sibling(index.row() , 0) ;
}
QStyleOptionViewItem opt ;
QSize s = sizeHint(opt , idx) ;
QString value = idx.data().toString() ;
QRectF rectF = painter->boundingRect ( opt.rect , Qt::AlignLeft, value ) ;
painter->drawText(rectF, value );
} else {
QItemDelegate::paint(painter , option , index) ;
}
}
QSize sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
{
return QItemDelegate::sizeHint(option, index);
}
};
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?
5267
Any suggestions out there?