Qt-interest Archive, July 2007
QComboBox + model + delegate
Message 1 in thread
Dear Experts,
i'm just wondering whether there was a change in QComboBox + associated
model/view stuff. I have implemented into my stuff
model and view (derived from QAbstractModelItem/QItemDelegate) which
displays set of various line styles. The code in delegate:
/*!
\fn Q_lineTypeDelegate::paint ( QPainter * painter, const
QStyleOptionViewItem & option, const QModelIndex & index ) const
paints the line on the painter surface
*/
void Q_lineTypeDelegate::paint ( QPainter * p, const QStyleOptionViewItem &
opt, const QModelIndex & index ) const
{
// copy of constant qualifier
QStyleOptionViewItem option = opt;
// selected stuff:
if (option.state & QStyle::State_Selected)
p->fillRect(option.rect, option.palette.highlight());
// draws standard text if no userrole is defined
QItemDelegate::paint (p, option, index);
// now we display our specialities:
// try if we have assigned color box to this item. This is indicated by
UserRole flag
QVariant col = index.data (Qt::UserRole);
if (col.isValid())
{
QRect editRect = option.rect;
editRect.adjust(4,4, -4,-4);
qDebug ()<< "our rectangle " << editRect << "original rectangle: "
<< opt.rect;
// message processed, don't do anything anymore
p->save();
p->setRenderHint(QPainter::Antialiasing, true);
// select pen type. See from constructor that we are starting at '1'
but
// current index starts at zero. thus we need to add '1' to the
current index
QPen types((Qt::PenStyle)col.toInt());
types.setWidth(2);
p->setPen (types);
p->drawLine(editRect.left(), (editRect.top()+editRect.bottom())/2,
editRect.right(), (editRect.top()+editRect.bottom())/2);
p->restore();
return;
}
}
This code works perfectly for Qt4.2, but does not work correctly for Qt4.3.0
When combo is created, its contents is ok. When I press down-arrow attached
to combo, listbox appears where all lines should be shown. Well, in fact, in
QT42 this is the case,
but in QT4.3 it seems that my function receives weird size information in
opt.rect field. The width is always set to zero. Why that?
This is what I get from qDebug line:
our rectangle QRect(4,4 680x8) original rectangle: QRect(0,0 688x16)
our rectangle QRect(4,20 -8x8) original rectangle: QRect(0,16 0x16)
our rectangle QRect(4,36 -8x8) original rectangle: QRect(0,32 0x16)
our rectangle QRect(4,52 -8x8) original rectangle: QRect(0,48 0x16)
our rectangle QRect(4,68 -8x8) original rectangle: QRect(0,64 0x16)
our rectangle QRect(4,84 -8x8) original rectangle: QRect(0,80 0x16)
our rectangle QRect(4,4 680x8) original rectangle: QRect(0,0 688x16)
there were totally 6 items in the list. Model returned for first item
QVariant (QString()) and for the others empty variant for DisplayRole,
however penstyle for UserRole. The width
of non-string stuff is zero....
thx
d.