2011-02-02 57 views
0

大家好,Extrend QAbstractListModel顯示自定義背景顏色?

http://oi51.tinypic.com/6hrm9w.jpg

我已經延長我自己QAbstractListModel改變QCombobox的背景色。如圖所示,我有兩個問題。 1)如第一張圖像快照所示,背景顏色不會出現在所選項目中。 2)選擇項目時,背景變爲默認高亮顏色(淺藍色)

是否有解決這兩個問題的方法?

這是我的QAbstractListModel實現。



RzContourLabelModel::RzContourLabelModel(RzContourLabelContext *contourLabelCtx,int max,QObject *parent) : QAbstractListModel(parent){ 


    contourCtx=contourLabelCtx; 
    QList contourLabels=contourLabelCtx->getLabels(); 

    for(int i=0;i= colorLabels.size()) 
       return QVariant(); 

     if (role == Qt::DisplayRole){ 
      QString str; 
      str.setNum(colorLabels.at(index.row())); 
      return str; 
     } 

     if (role == Qt::BackgroundRole) 
     { 
      int labelNum=colorLabels.at(index.row()); 
      QColor col= contourCtx->getLabelColor(labelNum); 
      return col; 
     } 
       return QVariant(); 
} 
 
+0

如何不想按顏色可視化選擇?也許嘗試用setSelectionMode(NoSelection)禁用選擇。不知道是否QComboBox可以處理。 – 2011-02-02 07:06:34

回答

2

這兩個功能(所選項目的背景和突出顯示的顏色)由視圖控制。下面是從文檔報價:

對於在組合框中標籤文本和圖標,用於在具有Qt::DisplayRoleQt::DecorationRole模型中的數據。

所以所選項目的背景不會很容易改變。相反,您可能需要製作顏色圖標並將其作爲模型中的Qt::DecorationRole返回。

對於突出顯示的顏色 - 您可以使用自定義項目代理重新實現此功能。見QComboBox::setItemDelegate

2

您可以嘗試使用Qt CSS玩...

不知道這幫助你,但它可以:

QComboBox QAbstractItemView { 
    selection-background-color: Transparent; 
    selection-color: Black; 
} 

它上色選擇阻止,唯一的事情我不知道關於 - 是否會在選擇的情況下繪製小部件的背景或您的項目的背景。如果它將繪製小部件的背景 - 它是無用的:(