(Porting old code - 5.14.2) Custom model derived from QAbstractItemModel receiving the wrong information?
-
Hey all, I'm under the need to add some functions on the fly, so I started looking at this old code which derives from QAbstractItemModel, theres a vector which holds the color of the row, setData looks like this:
bool ModeloComprobantes::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.row() > datos.size() || index.column()>titulos.size()) return false; if (!index.isValid()) return false; QVector<int> roles; if (role == Qt::EditRole) { datos[index.row()].insert(titulos[index.column()],value); roles.append(role); } if (role == Qt::BackgroundColorRole) { qDebug() << "ModeloComprobantes::setData(backgroundcolorrole):" << value; colorFila[index.row()] = QColor(value.toInt()); roles.append(role); } emit dataChanged(index,index,roles); return true; }
However, even though setData sends QColor("green") through the following sentence:
model->setData(model->index(index.row(),2/* column*/,QColor("green"),Qt::BackgroundColorRole);
The qDebug() line in the custom model tells me that the received color is QColor(ARGB 1, 0, 0, 0.501961, 0) which is seen black on the QTableView.
I do realize that the Role name has changed, so adding this feature in the old codebase having to migrate it over seems like a lot of lost time, however I wish to know what I'm doing wrong for the sake of completeness
Can anybody point me out?
-
Hey all, I'm under the need to add some functions on the fly, so I started looking at this old code which derives from QAbstractItemModel, theres a vector which holds the color of the row, setData looks like this:
bool ModeloComprobantes::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.row() > datos.size() || index.column()>titulos.size()) return false; if (!index.isValid()) return false; QVector<int> roles; if (role == Qt::EditRole) { datos[index.row()].insert(titulos[index.column()],value); roles.append(role); } if (role == Qt::BackgroundColorRole) { qDebug() << "ModeloComprobantes::setData(backgroundcolorrole):" << value; colorFila[index.row()] = QColor(value.toInt()); roles.append(role); } emit dataChanged(index,index,roles); return true; }
However, even though setData sends QColor("green") through the following sentence:
model->setData(model->index(index.row(),2/* column*/,QColor("green"),Qt::BackgroundColorRole);
The qDebug() line in the custom model tells me that the received color is QColor(ARGB 1, 0, 0, 0.501961, 0) which is seen black on the QTableView.
I do realize that the Role name has changed, so adding this feature in the old codebase having to migrate it over seems like a lot of lost time, however I wish to know what I'm doing wrong for the sake of completeness
Can anybody point me out?
@linuxkid said in (Porting old code - 5.14.2) Custom model derived from QAbstractItemModel receiving the wrong information?:
QColor(value.toInt());
This is wrong - you convert a QVariant to an integer and assign it a QColor.
QColor(value.value<QColor>());
-
@linuxkid said in (Porting old code - 5.14.2) Custom model derived from QAbstractItemModel receiving the wrong information?:
QColor(value.toInt());
This is wrong - you convert a QVariant to an integer and assign it a QColor.
QColor(value.value<QColor>());
@Christian-Ehrlicher Christian, thanks again, I edited the code to send a QVariant(color.rgba()) and on the receiving end take it in as QColor(value.toUint())
Thanks again for your time, I'm marking this one as solved!
-
-
@Christian-Ehrlicher Christian, thanks again, I edited the code to send a QVariant(color.rgba()) and on the receiving end take it in as QColor(value.toUint())
Thanks again for your time, I'm marking this one as solved!
@linuxkid said in (Porting old code - 5.14.2) Custom model derived from QAbstractItemModel receiving the wrong information?:
nd take it in as QColor(value.toUint())
Use my solution as this is the correct one.