Why i can't change color of QString?
-
@mrjj said in Why i can't change color of QString?:
Ah, if you change model, you will have to Color that index also.
Tried that just after you post your example with setItemData.
Now i try to figure out how use this not to comboBox, but to the model. But setItemData from model is much more complex( i can't get how 2nd argument should looks like).
-
@Engelard
You have you own std. item models you use with it ?Anyway, this also seems to work
auto model = ui->comboBox->model();
model->setData( model->index(4, 0), QColor(255, 0, 0), Qt::TextColorRole );
@mrjj I did'nt quite understand your question, here is how whole code looks like:
QComboBox *tempBx = ui->comboBox1; tempBx->setModel(new QStringListModel(someList)); tempBx->setCurrentIndex(indx); tempBx->setItemData(indx, QColor(Qt::red), Qt::TextColorRole);Everything is working as it should, but not that part which should change colors..
@mrjj said in Why i can't change color of QString?:
model->setData( model->index(4, 0), QColor(255, 0, 0), Qt::TextColorRole );
Ah it is setData, not item data. But also, no effect....
-
Hi
Tested with QStringListModel and you are right. It does not color it.
Same code with inserted items from Designer works.
So its seems it wont work with QStringListModel. -
@mrjj I did'nt quite understand your question, here is how whole code looks like:
QComboBox *tempBx = ui->comboBox1; tempBx->setModel(new QStringListModel(someList)); tempBx->setCurrentIndex(indx); tempBx->setItemData(indx, QColor(Qt::red), Qt::TextColorRole);Everything is working as it should, but not that part which should change colors..
@mrjj said in Why i can't change color of QString?:
model->setData( model->index(4, 0), QColor(255, 0, 0), Qt::TextColorRole );
Ah it is setData, not item data. But also, no effect....
@Engelard
QStringListModel only supports DisplayRole and EditRole.You can however quickly implement a simple QAbstractListModel and add support for multiple other roles (btw you should use Qt::ForegroundRole)
-
Hi
As @raven-worx says, a QAbstractListModel would be the cleanest solution.You could also cheat and do.
QStringList StrList{"test" , "test2" , "test3" }; QStandardItemModel *model = new QStandardItemModel (StrList.size(), 1); for (int row = 0; row < StrList.size(); ++row) { QStandardItem *item = new QStandardItem(QString("row %0").arg(row)); model->setItem(row, 0, item); } ui->comboBox->setModel(model); model->setData( model->index(0, 0), QColor(255, 0, 0), Qt::TextColorRole ); model->setData( model->index(1, 0), QColor(0, 255, 0), Qt::TextColorRole );
-
Thanks @raven-worx && @mrjj for help. It is like 8-times more code now, i can't rly understand why it don't work with simple QStringListModel, but it is at least some success today.
It change color in list of ComboBox, but not in actual CB, in @mrjj screenshot such thing also present:

P.S. in my initial priority was to display colored in mainWidget of ComboBox rather then in opened list.
-
Hi
- i can't rly understand why it don't work with simple QStringListModel
Well @raven-worx was kind enough to show the source
so we can see how QStringListModel worksQVariant QStringListModel::data(const QModelIndex &index, int role) const { if (index.row() < 0 || index.row() >= lst.size()) return QVariant(); if (role == Qt::DisplayRole || role == Qt::EditRole) return lst.at(index.row()); return QVariant(); }The data function of a model IS the place where it returns the data for the roles so we can see
it only really uses (role == Qt::DisplayRole || role == Qt::EditRole) and
we want it to also use Qt::ForegroundRole. -
I made a proxy model to support roles in models that don't https://github.com/VSRonin/QtModelUtilities the class is called
RoleMaskProxyModel. Here is an example that uses it to colourQStringListModel: https://github.com/VSRonin/QtModelUtilities/blob/1.0.0/examples/exam_RoleMaskProxyModel/exam_rolemaskhighlight.cppHopefully I'll eventually find the time to complete adding that class to Qt: https://codereview.qt-project.org/#/c/245572/ -
I made a proxy model to support roles in models that don't https://github.com/VSRonin/QtModelUtilities the class is called
RoleMaskProxyModel. Here is an example that uses it to colourQStringListModel: https://github.com/VSRonin/QtModelUtilities/blob/1.0.0/examples/exam_RoleMaskProxyModel/exam_rolemaskhighlight.cppHopefully I'll eventually find the time to complete adding that class to Qt: https://codereview.qt-project.org/#/c/245572/@VRonin Looks very interesting.
I downloaded your rolemaskproxymodel and classes related to it(whole src directory), but when i tried to Run i got such strange stuff:
Oh and don't look at first two errors, they unrelated completelly, it appears when i swap compiler to Release mode, did'nt figure out what it is yet. -
If you just copy-paste the code you have to add
DEFINES += MODELUTILITIES_STATICto your .pro file -
Exact same answer.
There's a section in the readme that explains how to use the library: https://vsronin.github.io/QtModelUtilities/md__i_n_s_t_a_l_l.html -
Exact same answer.
There's a section in the readme that explains how to use the library: https://vsronin.github.io/QtModelUtilities/md__i_n_s_t_a_l_l.html -
You have to manually click on build-> rerun qmake every time you change something in the .pro file
-
yep!
AllDEFINES += MODELUTILITIES_STATICdoes is transformingMODELUTILITIES_EXPORTinto an empty string. if that doesn't work then just manually do a find and replace