开发者

Extrend QAbstractListModel to display custom background color?

开发者 https://www.devze.com 2023-02-08 04:58 出处:网络
Greetings all, I have extended my own QAbstractListModel to change the background color of QCombobox. As seen it the image,I have two issues.

Greetings all,

Extrend QAbstractListModel to display custom background color?

I have extended my own QAbstractListModel to change the background color of QCombobox. As seen it the image ,I have two issues. 1) As shown in the first image snapshot , background color doesnt appear for the selected item. 2) When selecting items , the background turns in to default highlight color (light blue)

Is there anyway to fix these two issues ?

Here is my QAbstractListModel implementation.



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


    contourCtx=contourLabelCtx;
    QLis开发者_StackOverflow社区t 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();
 }


Both features (background of the selected item and highlighting color) are controlled by the view. Here is a quote from the docs:

For the text and icon in the combobox label, the data in the model that has the Qt::DisplayRole and Qt::DecorationRole is used.

So the background of the selected item wont be easy to change. Instead you might want to make color icons and return them as the Qt::DecorationRole in the model.

For the highlighting color - you can reimplement this with a custom item delegate. See QComboBox::setItemDelegate


You may try to play with Qt CSS...

Not sure this help you but it may:

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

it prevents from colorizing selection, the only thing i am not sure about - will it paint your widget's background or your item's background in case of selection. If it will paint widget's background - it is useless :(

0

精彩评论

暂无评论...
验证码 换一张
取 消