Well, I still don't quite have it, but we are close! I added the
QModelIndex sourceIndex = mapToSource(index);
To copy to clipboard, switch view to plain text mode
to the data() function and the correct cell is highlighted. But when the sort is conducted the highlight is on the same cell and not on the correct data. I figured that the sourceIndex needed to be used for the data() and when I used the following code the highlight now is linked to the data. BUT the wrong data is highlighted. Here is the code:
{
if (!sourceIndex.isValid())
if ( sourceIndex.row() == m_fastRow && sourceIndex.column()== 32 && role == Qt::BackgroundRole )
{
}
else if ( sourceIndex.row() == m_slowRow && sourceIndex.column()== 32 && role == Qt::BackgroundRole )
{
}
else
{
}
}
QVariant MyProxyModel::data ( const QModelIndex & index, int role ) const
{
QModelIndex sourceIndex = mapToSource(index);
if (!sourceIndex.isValid())
return QVariant();
if ( sourceIndex.row() == m_fastRow && sourceIndex.column()== 32 && role == Qt::BackgroundRole )
{
return QVariant( Qt::yellow );
}
else if ( sourceIndex.row() == m_slowRow && sourceIndex.column()== 32 && role == Qt::BackgroundRole )
{
return QVariant( Qt::red );
}
else
{
return QSortFilterProxyModel::data( index, role );
}
}
To copy to clipboard, switch view to plain text mode
I think that somehow I need to mapToSource or mapFromSource in my code that figures out the correct row. In debugging the code it appears that one model is the reverse order of the other. Here is the code I use for getting the row.
printModel-> setTable (mTableName);
printModel
-> setRelation
(2,
QSqlRelation("rider",
"id",
"LName"));
printModel->select();
proxy = new MyProxyModel(this);
proxy->setSourceModel(printModel);
ui->printView->setModel(proxy);
printModel->setFilter(mFilterString);
printModel->select();
int numRows =proxy->rowCount();
for (int r=0; r<numRows; r++ )
{
currentRunTime = proxy->index(r,32).data(Qt::DisplayRole).toFloat();
if (currentRunTime < fastRunTime)
{
fastRunTime = currentRunTime;
rowFast = r;
}
if (currentRunTime > slowRunTime)
{
slowRunTime = currentRunTime;
rowSlow = r;
}
}
emit sendRows (rowSlow, rowFast);
printModel= new QSqlRelationalTableModel (this);
printModel-> setEditStrategy(QSqlTableModel::OnRowChange);
printModel-> setTable (mTableName);
printModel-> setRelation (2, QSqlRelation("rider", "id", "LName"));
printModel->select();
proxy = new MyProxyModel(this);
proxy->setSourceModel(printModel);
ui->printView->setModel(proxy);
ui->printView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->printView->setSelectionBehavior(QAbstractItemView::SelectRows);
printModel->setFilter(mFilterString);
printModel->select();
int numRows =proxy->rowCount();
for (int r=0; r<numRows; r++ )
{
currentRunTime = proxy->index(r,32).data(Qt::DisplayRole).toFloat();
if (currentRunTime < fastRunTime)
{
fastRunTime = currentRunTime;
rowFast = r;
}
if (currentRunTime > slowRunTime)
{
slowRunTime = currentRunTime;
rowSlow = r;
}
}
emit sendRows (rowSlow, rowFast);
To copy to clipboard, switch view to plain text mode
Any ideas?
Bookmarks