I am using this Example http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html and need to pass a Color as Forgoundroll to the data, but cant figure it out.
In the treemodel.cpp i have altered the data as following..
QVariant TreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole && role != Qt::EditRole && role != Qt::ForegroundRole)
return QVariant();
TreeItem *item = getItem(index);
if(role==Qt::ForegroundRole) {
QBrush redBackground(QColor(Qt::red));
return redBackground;
} else
return item->data(index.column());
}
... which works (items get the red color, but need to control the color from the mainwindow.cpp and let user set it and have different colors per column/row. Apparently i need to alter the Treemodel:setdata method, but cant figure it out.
So seeking for the setdata method..
bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role != Qt::EditRole && role != Qt::ForegroundRole )
return false;
TreeItem *item = getItem(index);
bool result;
if(role==Qt::ForegroundRole ) {
//do what ???????
} else {
result= item->setData(index.column(), value);
}
if (result) emit dataChanged(index, index);
return result;
}
From mainwindow.cpp i need to set it like ..
model->setData(child, QVariant(rowdata.at(column)), Qt::EditRole); // this provides the text of the inserted row
model->setData(child, QVariant(QBrush(Qt::red)), Qt::ForegroundRole); // this to provide its color
... but i get the text of the color #ffffff instead (in red color though). Any help would be appreciated. Thanks