I used the QModelIndex's model to color the cell using the setData method that was mentioned above, but as part of slot rather than a delegate. This will highlight whatever cell the user clicks on.
void MyParentWidget::init()
{
this, SLOT(highlightCell(const QModelIndex&)));
}
void MyParentWidget::init()
{
connect(((QAbstractItemView*)this->table), SIGNAL(pressed(const QModelIndex&)),
this, SLOT(highlightCell(const QModelIndex&)));
}
To copy to clipboard, switch view to plain text mode
void MyParentWidget
::highlightCell(const QModelIndex &cellIndex
) {
for(int i=0; i<cellIndex.model()->columnCount(); i++)
{
for(int j=0; j<cellIndex.model()->rowCount(); j++)
{
if(i == cellIndex.column() && j == cellIndex.row())
{
Qt::BackgroundRole);
}
else
{
Qt::BackgroundRole);
}
}
}
}
void MyParentWidget::highlightCell(const QModelIndex &cellIndex)
{
for(int i=0; i<cellIndex.model()->columnCount(); i++)
{
for(int j=0; j<cellIndex.model()->rowCount(); j++)
{
if(i == cellIndex.column() && j == cellIndex.row())
{
((QStandardItemModel*)cellIndex.model())->item(cellIndex.row(), i)->setData(QBrush(Qt::yellow),
Qt::BackgroundRole);
}
else
{
((QStandardItemModel*)cellIndex.model())->item(cellIndex.row(), i)->setData(QBrush(Qt::white),
Qt::BackgroundRole);
}
}
}
}
To copy to clipboard, switch view to plain text mode
You can modify & extend this behavior to meet your needs.
Bookmarks