PDA

View Full Version : QCheckBox in QTreeWidget



Kode.Cooper
25th March 2010, 03:35
Hi there,
I'm add a QCheckBox in my QTreeWidget like these, but, How to set the widget to be center of the column rect?


treeWidget->setItemWidget(subItem, 2, chkItem);
subItem->setTextAlignment(2, Qt::AlignHCenter);
Qt::CheckState chkState = (bNeedSync.toInt() == 1) ? Qt::Checked : Qt::Unchecked;
chkItem->setCheckState(chkState);

I digged the code and this forum and no without any tip information. Thanks.
4449

aamer4yu
25th March 2010, 04:00
You will need to use your own delegate. Look for QItemDelegate and QStyledItemDelegate. In that you will need to set the position for check and draw.

Kode.Cooper
25th March 2010, 05:24
You will need to use your own delegate. Look for QItemDelegate and QStyledItemDelegate. In that you will need to set the position for check and draw.

Accturally, I don't know how to set the rect of the checkbox item. I use QTreeWidget with QStyledItemDelegate instead of QTreeView, you know, in Model-View, I can control every thing I need.
But in QTreeWidget, It seems not easy for handle this.

I wrote an example in my QStyleItemDelegate subclass,


void CMTreeDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
Q_ASSERT(index.isValid());

QStyleOptionViewItem opt = option;
// draw the check box
if ( index.column() == 2 )
{
return;
}

QStyledItemDelegate::paint(painter, opt, index);
}

The check box still showen in the column. obviousely, the delegate can not control the rect of the Checkbox which created with


treeWidget->setItemWidget(subItem, 2, chkItem);
subItem->setTextAlignment(2, Qt::AlignHCenter);
Qt::CheckState chkState = (bNeedSync.toInt() == 1) ? Qt::Checked : Qt::Unchecked;
chkItem->setCheckState(chkState);

aamer4yu
25th March 2010, 05:55
Am not sure how to do with QStyledItemDelegate. But with QItemDelegate you need to set the rect in option.,, and then call drawCheck.
Look into Qt source code,,, you will get an idea of what calls are done to do the drawing