问题
I'm having some difficulty adding a delegate to my QTreeView. I have added some QStandardItems through a model which works fine, but when I add the delegate, the text is erased and only the icons are visible.
This is the code i'm using for my delegate:
void SeqNavDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.column() == 0 && option.state & QStyle::State_Enabled)
{
const QIcon icon(QLatin1String(":/SeqNavMenu/images/green.png"));
QRect iconRect(option.rect.right() - option.rect.height(),
option.rect.top(),
option.rect.height(),
option.rect.height());
icon.paint(painter, iconRect, Qt::AlignRight);
}
}
What i would like to do is combine the two, which is to say, have the text and checkboxes, and to the right have the icons that i have put in the delegate.
Maybe someone can point me in the right direction here ?
Cheers.
回答1:
When you assign a delegate to a view, the view stops rendering items by itself (actually it does it with another delegate which is replaced by yours). So it delegates the rendering to you. And you programed the delegate to draw icons only. Thats why you see only icons.
If you need to draw a check-box and a text as well you need to draw it by yourself or call the ancestors method paint
somewhere in your implementation. So if you inherited SeqNavDelegate
from QStyledItemDelegate
call :
QStyledItemDelegate::paint(painter, option, index);
来源:https://stackoverflow.com/questions/25426793/delegate-erasing-text-in-qtreeview-using-qstandarditemmodel