PDA

View Full Version : Wrong painting of Delegate



nifei
5th February 2009, 06:26
Hi, all, i sub class QStyledItemDelegate and re-implement the paint () function like this:

void CustomDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index)const
{
painter->save();
painter->fillRect(option.rect, QBrush(Qt::red));
painter->setPen(QColor(Qt::black));
painter->drawRect(option.rect);
painter->restore();
QStyledItemDelegate::paint(painter, option, text, false);
}

Sadly the delegate paint the item beyond the boder of the QListView and looks like the attachment. I don't know why. The picture in the attachment is a screen shotcut when the cursor hover the area that is very near to the list veiw's bottom border, outside the view.

Any help and suggestions will be appreciated greatly, thank you in advance.

aamer4yu
5th February 2009, 06:40
Try re-ordering ur steps as -


painter->fillRect(option.rect, QBrush(Qt::red));
QStyledItemDelegate::paint(painter, option, text, false);
painter->save();
painter->setPen(QColor(Qt::black));
painter->drawRect(option.rect);
painter->restore();


Am not sure if it will work, but the idea is the border rect will be on top...

nifei
5th February 2009, 06:47
Try re-ordering ur steps as -


painter->fillRect(option.rect, QBrush(Qt::red));
QStyledItemDelegate::paint(painter, option, text, false);
painter->save();
painter->setPen(QColor(Qt::black));
painter->drawRect(option.rect);
painter->restore();


Am not sure if it will work, but the idea is the border rect will be on top...

Thanks for your advice.

I tried as yours and the issue remain the same. And i remove the calling of QStyledItemDelegate::paint(...) and the red area outside the view still exists.

nifei
5th February 2009, 07:51
I used QStyledItemDelegate and the issue remain! Here's my code:


/* listView is a QListView*/
/* set item's data before set model */
for(...)
{
QStandardItem *item = new QStandardItem();
item->setText("test");
item->setSizeHint(Qsize(100, 50));
model->appendRow(item);
}
...
listView->setModel(model);//no setting delegate is called



and i set the style sheet as


QListView::item
{
background-color:red;
}


It astonished me that the issue is not solved!

BTW, this list view is a child widget of another widget which proxyed by a QGraphicsItem. I'm currently wondering whether or not that is the reason