Solved Qtableview color column
-
i need to set color to column using data() but this code is not working why ?
QVariant tool:: data(const QModelIndex& index, int role) const { QAbstractItemModel* table1 = ui.tableView->model(); int col = table1->columnCount(); switch (role) { case Qt::BackgroundRole: { switch (col) { case 1: return QColor(Qt::yellow); case 2: return QColor(Qt::yellow); } } default: break; } return QVariant(); }
Thanks in advance.
-
for (int row = 0; row < table1->rowCount(); ++row) { for (int column : {1,2,3,4,5,9,10}) table1->setData(table1->index(row, column), QBrush(QColor(240, 248, 255)), Qt::BackgroundRole); } QObject::connect(table1, &QAbstractItemModel::rowsInserted, table1, [table1](const QModelIndex& parent, int first, int last) { for (int row = first;row <= last;++row){ for (int column : {1,2,3,4,5,9,10}) table1->setData(table1->index(row,column,parent),QBrush(QColor(240, 248, 255)), Qt::BackgroundRole); } });
-
@n-2204 said in Qtableview color column:
this code is not working why ?
int col = table1->columnCount();
-
@VRonin this i am using to get column no. what to use instead of this ?
-
@n-2204 said in Qtableview color column:
this i am using to get column no
No, think, what that line does. If it helps check with a debugger what its value is
-
@VRonin ohk
looks like even not going in this part of code as i haven't connect this -
@VRonin using this code i am able color the column and row but when i am adding new row color is not updated to specified column
for (int row = 0; row < table1->rowCount(); ++row) { for (int column = 0; column < table1->columnCount(); ++column) { if (column == 1 || column == 2 || column == 3 || column == 4 || column == 5 || column == 9 || column == 10) { const QModelIndex index1 = model->index(row, column); table1->setData(index1, QColor(240, 248, 255), Qt::BackgroundRole); QObject::connect(table1, &QAbstractItemModel::rowsInserted, table1, [table1] (const QModelIndex& parent, int first, int last) { for (int i = first;i <= last;++i) what to pass in this index()? so when add row color should be added on the mentioned column table1->setData(index(),QColor(240, 248, 255), Qt::BackgroundRole); }); } } }
-
for (int row = 0; row < table1->rowCount(); ++row) { for (int column : {1,2,3,4,5,9,10}) table1->setData(table1->index(row, column), QBrush(QColor(240, 248, 255)), Qt::BackgroundRole); } QObject::connect(table1, &QAbstractItemModel::rowsInserted, table1, [table1](const QModelIndex& parent, int first, int last) { for (int row = first;row <= last;++row){ for (int column : {1,2,3,4,5,9,10}) table1->setData(table1->index(row,column,parent),QBrush(QColor(240, 248, 255)), Qt::BackgroundRole); } });