I have a QListWidget with several QListWidgetItems containing only text but with different background colors. By default the items become highlighted with a blue bar when I mouse over them. How can I disable the highlighting?
The code I use
//add spacer
QListWidgetItem *spacer = new QListWidgetItem("foo");
spacer->setBackgroundColor(QColor(Qt::gray));
spacer->setFlags(Qt::ItemIsEnabled); //disables selectionable
ui->listWidget->addItem(spacer);
Thanks in advance.
spacer
is the gray item with the name of the day
EDIT: added picture link (snipping tool Tool hides mouse, 6th item is highlighted)
I managed to do it with an custom item delegate overriding the QStyledItemDelegate::paint
method.
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);
}