I have a QTableView that is based on a SQLRelational model. I subclassed QSortFilterProxyModel to be able to use the data() function to color certain cells based on a previous determination of the row. I was able to sucessfully do that, however if the user sorts the table by clicking on the header, the table sorts properly but the cells (not the correct data) that were previously highlighted are still highlighted. How do I get the data() function to get called after the sort is completed? I thought that I could emit a dataChanged signal, but it doesn't seem to work. Is this the proper way of doing this or is there a better way?
MainClass.h
#include <QSqlRelationalTableModel>
#include<QSortFilterProxyModel>
#include "myproxymodel.h"
MyProxyModel* proxy;
class MyProxyModel;
#include <QSqlRelationalTableModel>
#include<QSortFilterProxyModel>
#include "myproxymodel.h"
QSqlRelationalTableModel *printModel;
MyProxyModel* proxy;
QHeaderView *horizHeader;
class MyProxyModel;
To copy to clipboard, switch view to plain text mode
MainClass.cpp
void MainClass
::createReportTable(QStringList stringList
) {
mReportOptions=stringList;
printModel-> setTable (mTableName);
printModel
-> setRelation
(2,
QSqlRelation("LName",
"id",
"Last Name"));
printModel->select();
proxy = new MyProxyModel(this);
proxy->setSourceModel(printModel);
ui->printView->setModel(proxy);
horizHeader= ui->printView->horizontalHeader();
connect(this,SIGNAL(sendRows(int, int)), // to send the selected rows to the proxy
proxy, SLOT(getRows(int, int)));
connect(horizHeader, SIGNAL(sortIndicatorChanged (int) ), //This dooesn't seem to work
proxy,SIGNAL(dataChanged ( ) )); //this was to capture the click on the header and repopulate the table
printModel->setFilter(mFilterString);
printModel->select();
highlightCells();
void MainClass::highlightCells()
{
float lowSale=FLT_MAX;
float highSale=FLT_MIN;
float currentSales;
int rowHigh=0;
int rowLow=0;
int numRows =proxy->rowCount();
for (int r=0; r<numRows; r++ )
{
if (ui->printView->isRowHidden(r))
continue;
currentSales = proxy->index(r,5).data(Qt::DisplayRole).toFloat();
if (currentSales < lowSale)
{
lowSale = currentSale;
rowLow = r;
}
if (currentSale > highSale)
{
highSale = currentSale;
rowHigh = r;
}
}
emit sendRows (rowSlow, rowFast);
}
void MainClass::createReportTable(QStringList stringList)
{
mReportOptions=stringList;
printModel= new QSqlRelationalTableModel (this);
printModel-> setEditStrategy(QSqlTableModel::OnRowChange);
printModel-> setTable (mTableName);
printModel-> setRelation (2, QSqlRelation("LName", "id", "Last Name"));
printModel->select();
proxy = new MyProxyModel(this);
proxy->setSourceModel(printModel);
ui->printView->setModel(proxy);
horizHeader= ui->printView->horizontalHeader();
connect(this,SIGNAL(sendRows(int, int)), // to send the selected rows to the proxy
proxy, SLOT(getRows(int, int)));
connect(horizHeader, SIGNAL(sortIndicatorChanged (int) ), //This dooesn't seem to work
proxy,SIGNAL(dataChanged ( ) )); //this was to capture the click on the header and repopulate the table
ui->printView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->printView->setSelectionBehavior(QAbstractItemView::SelectRows);
printModel->setFilter(mFilterString);
printModel->select();
highlightCells();
void MainClass::highlightCells()
{
float lowSale=FLT_MAX;
float highSale=FLT_MIN;
float currentSales;
int rowHigh=0;
int rowLow=0;
int numRows =proxy->rowCount();
for (int r=0; r<numRows; r++ )
{
if (ui->printView->isRowHidden(r))
continue;
currentSales = proxy->index(r,5).data(Qt::DisplayRole).toFloat();
if (currentSales < lowSale)
{
lowSale = currentSale;
rowLow = r;
}
if (currentSale > highSale)
{
highSale = currentSale;
rowHigh = r;
}
}
emit sendRows (rowSlow, rowFast);
}
To copy to clipboard, switch view to plain text mode
myproxymodel.cpp
#include "myproxymodel.h"
MyProxyModel
::MyProxyModel (QObject *parent
) :{
}
MyProxyModel::~MyProxyModel()
{
}
{
if (!index.isValid())
if ( index.row() == m_lowRow && index.column()== 5 && role == Qt::BackgroundRole )
{
}
else if ( index.row() == m_highRow && index.column()== 5 && role == Qt::BackgroundRole )
{
}
else
{
}
}
void MyProxyModel::getRows( int rowHigh,int rowLow)
{
m_highRow=rowHigh;
m_lowRow=rowLow;
}
#include "myproxymodel.h"
MyProxyModel::MyProxyModel (QObject *parent) :
QSortFilterProxyModel(parent)
{
}
MyProxyModel::~MyProxyModel()
{
}
QVariant MyProxyModel::data ( const QModelIndex & index, int role ) const
{
if (!index.isValid())
return QVariant();
if ( index.row() == m_lowRow && index.column()== 5 && role == Qt::BackgroundRole )
{
return QVariant( Qt::yellow );
}
else if ( index.row() == m_highRow && index.column()== 5 && role == Qt::BackgroundRole )
{
return QVariant( Qt::red );
}
else
{
return QSortFilterProxyModel::data( index, role );
}
}
void MyProxyModel::getRows( int rowHigh,int rowLow)
{
m_highRow=rowHigh;
m_lowRow=rowLow;
}
To copy to clipboard, switch view to plain text mode
So how do I get the proper data to be highlighted after the table is sorted?
Bookmarks