我设法通过覆盖该QStyledItemDelegate::paint
方法的自定义项目委托来做到这一点。
void ActivitiesItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
//determine if index is spacer
//TODO non-hacky spacer detection
bool spacer = false;
QString text = index.data().toString();
if( ( text == "Monday")
|| ( text == "Tuesday")
|| ( text == "Wednesday")
|| ( text == "Thursday")
|| ( text == "Friday")
|| ( text == "Saturday")
|| ( text == "Sunday")
){
spacer = true;
}
if(option.state & QStyle::State_MouseOver){
if(spacer){
painter->fillRect(option.rect, QColor(Qt::gray));
}else{
painter->fillRect(option.rect, QColor(Qt::white));
}
painter->drawText(option.rect.adjusted(3,1,0,0), text);
return;
}
//default
QStyledItemDelegate::paint(painter, option, index);
}