How to set background color in QComboBox?
-
Hello,
I would like something like that:
When I add next item to QComboBox, I would like to set its background color ( don't change that later ). Example:EDIT
And I would like one more thing:
When I choose "two" I would like that the choosen background item will be in the same color ( red ). In this example the choosen item text is "one" ( I think about that "one" above beacuse there are 2 x "one" ). -
Hi
you can do it via the modelconst QModelIndex idx = colorComboBox->model()->index(x, y); colorComboBox->model()->setData(idx, Qt:red, Qt::BackgroundColorRole); colorComboBox->model()->setData(idx, Qt::blue, Qt::ForegroundRole);
-
@mrjj Thank you :)
This is exactly what I need.But I Edit my post, so I need one more thing:
"When I choose "two" I would like that the choosen background item will be in the same color ( red ). In this example the choosen item text is "one" ( I think about that "one" above beacuse there are 2 x "one" )."
Do you know how Can I do that? Maybe slot to signal currentIndexChanged(int index)? But how can I get the color of index "index"?
EDIT I try this:
box->model()->setData(idx, QColor(255,0,0), Qt::BackgroundColorRole); QColor color = (QColor)box->model()->data(idx,Qt::BackgroundColorRole);
But it doesn't work.
-
@mrjj Thank you :)
This is exactly what I need.But I Edit my post, so I need one more thing:
"When I choose "two" I would like that the choosen background item will be in the same color ( red ). In this example the choosen item text is "one" ( I think about that "one" above beacuse there are 2 x "one" )."
Do you know how Can I do that? Maybe slot to signal currentIndexChanged(int index)? But how can I get the color of index "index"?
EDIT I try this:
box->model()->setData(idx, QColor(255,0,0), Qt::BackgroundColorRole); QColor color = (QColor)box->model()->data(idx,Qt::BackgroundColorRole);
But it doesn't work.
You mean the item when dropbox is not open?
Its a lineEdit, so might need some fiddling. StyleSheet maybe.
Yes to currentIndexChanged.
You can use
const QModelIndex idx = colorComboBox->model()->index(index,0); (or reverse. check doc)
to get ModelIndex and read the color with Data function -
Yes
I have small problem with convert QVariant to QColor. But when I have to add parameter QVariant, QColor was fine :)