2017-02-22 2 views
0

텍스트가 있지만 배경색이 다른 여러 QListWidgetItems가있는 QListWidget이 있습니다. 기본적으로 항목 위에 마우스를 가져 가면 항목이 파란색 막대로 강조 표시됩니다. 강조 표시를 비활성화하려면 어떻게합니까?QListWidget mouseover highlight 비활성화

코드는 내가 사전에

//add spacer 
QListWidgetItem *spacer = new QListWidgetItem("foo"); 
spacer->setBackgroundColor(QColor(Qt::gray)); 
spacer->setFlags(Qt::ItemIsEnabled); //disables selectionable 
ui->listWidget->addItem(spacer); 

감사를 사용합니다. 당신은 배경을 무시할 수

답변

0

설명 나는 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); 
} 
0

추가 사진 링크 (자르는 것은 도구 도구는 마우스를 숨 깁니다, 6 번째 항목이 강조 표시) :

spacer

는 편집이

이 날의 이름으로 회색 아이템입니다 당신이 사용하는 경우 제공, Qt는 CSS와 "가져가" "회색"색상 :

spacer->setStylesheet("*:hover {background:gray;}"); 

또는 전체 목록으로 스타일링 Qt Style sheet examples

QListView::item:hover { 
    background: gray 
} 
+0

목록에 두 가지 배경색이 있습니다. 한 배경을 흰색으로, 다른 배경을 회색으로 정의하려면 어떻게해야합니까? – Tac0

+0

으로 두 번째 색상을 설정할 수 있습니다. QListView :: item : alternate { background : #EEEEEE; } –

+0

대체 속성을 사용하는 것이 올바르지 않습니다. 스페이서 항목 아래에 활동이 많으면 색이 잘못 적용됩니다. QListWidgetItems 텍스트를 기반으로 마우스 오버 배경을 설정하는 방법을 찾고 있습니다. 또는 QSS에서 참조 할 사용자 정의 유형 속성을 할당 할 수 있습니까? – Tac0