PDA

View Full Version : checkbox and radiobutton in treeview



ErmandoFerrari
8th May 2010, 19:20
Hi all
I have to show radio button and check box in a treeview.

to do this I have derived a QAbstractItemModel class that with a custom role that get/set the node check type (radio buttons or check box)
I also derived a QItemDelegate, on this new class in the virtual function:
drawCheck(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, Qt::CheckState state) const
is possible to draw directly the check box or the radio button with QApplication::style()->drawPrimitive using as parameter PE_IndicatorRadioButton or PE_IndicatorViewItemCheck.

my problem is: how can I access the item to draw? I need a ModelIndex to know if draw a radio button or a check box.

Thank all

ErmandoFerrari
8th May 2010, 20:33
I have find a patch:
since drawCheck is called by paint, I've reimplemented the paint function in QMyItemDelegate:

class QMyItemDelegate : public QItemDelegate
{
...
protected:
virtual void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
void drawCheck ( QPainter * painter, const QStyleOptionViewItem & option, const QRect & rect, Qt::CheckState state ) const;
private:
mutable int selType;
...
}

where selType is mutable becouse of the const attribute of the functions

void QMyItemDelegate ::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
selType=index.model()->data(index,SelTypeRole).toInt();
QItemDelegate::paint(painter,option,index );
}

void QMyItemDelegate ::drawCheck(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, Qt::CheckState state) const
{
...
switch(selType)
{
case stp_check:
style->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &opt, painter);
break;
case stp_radio:
style->drawPrimitive(QStyle::PE_IndicatorRadioButton, &opt, painter);
}

where SelTypeRole is a custom role that return stp_check or stp_radio.

That work, but is a little "dirt" for me... ;)

any other idea?

thanks