poporacer
22nd April 2011, 21:22
I posted this in the Newbie section and haven't gotten a response. I thought that maybe this was more than a newbie question. Sorry if I shouldn't have double posted. 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 tableview to have the properly highlighted rows after the sort? I thought that it might work if I get the data() function to get called after the sort is completed, but can't figure out how to do that. I tried to 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"
QSqlRelationalTableModel *printModel;
MyProxyModel* proxy;
QHeaderView *horizHeader;
class MyProxyModel;
MainClass.cpp
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::SingleSelectio n);
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);
}
myproxymodel.cpp
#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;
}
So how do I get the proper data to be highlighted after the table is sorted?
MainClass.h
#include <QSqlRelationalTableModel>
#include<QSortFilterProxyModel>
#include "myproxymodel.h"
QSqlRelationalTableModel *printModel;
MyProxyModel* proxy;
QHeaderView *horizHeader;
class MyProxyModel;
MainClass.cpp
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::SingleSelectio n);
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);
}
myproxymodel.cpp
#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;
}
So how do I get the proper data to be highlighted after the table is sorted?