QListView等視圖更改選中時文本顏色

1.QSS樣式表

只能統一設置,不夠靈活

2.通過代理

    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const  
    {  
        QStyleOptionViewItem  viewOption(option);  
        //高亮顯示與普通顯示時的前景色一致(即選中和未選中時候的文字顏色一樣)  
        if(index.data(Qt::UserRole) .toInt() == 1 && index.column() == 1)
      		  viewOption.palette.setColor(QPalette::HighlightedText, index.data(Qt::ForegroundRole).value<QColor>());  
        QItemDelegate::paint(painter, viewOption, index);  
    }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章