Ashika Umanga Umagiliya
Ashika Umanga Umagiliya

Reputation: 9168

Extrend QAbstractListModel to display custom background color?

Greetings all,

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

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;
    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();
 }

Upvotes: 0

Views: 1854

Answers (2)

Raiv
Raiv

Reputation: 5781

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 :(

Upvotes: 2

ak.
ak.

Reputation: 3449

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

Upvotes: 3

Related Questions