poporacer
16th August 2011, 04:19
I have a tableview controlled by a proxy. In the tableview, the highest and lowest values are highlighted through the data function in the proxy. I want the user to be able to delete a selected row (just from the view, not the source!) I have one method almost figured out. The problem is that when a row is deleted, the correct data is not highlighted. I think I need to use mapToSource and mapFromSource, but I haven't been able to get it to work properly.
This is the method I use...when the user deletes a row, the row is hidden in the tableView, then when the tableview is iterated to find the highest and lowest numbers, the hidden row is skipped. Then the identified rows are sent to the proxy to highlight the correct cells.
Is this the correct way or is there a better way? How do I get this to work?
Here is the code for the tableView creation:
testclass.cpp
void TestClass::createTimerTable() // this is called in the constructor of the form
{
testModel= new QSqlRelationalTableModel(this);
testModel->setEditStrategy(QSqlTableModel::OnRowChange);
testModel->setTable("testtable");
testModel->setRelation(2,QSqlRelation("student","id", "LName"));
testModel->setRelation(3,QSqlRelation("testNum", "id","Test"));
testModel->setHeaderData(1,Qt::Horizontal,"Section");
proxy = new MyProxyModel (this);
proxy->setSourceModel(testModel);
ui->tableView->setModel(proxy);
connect(this,SIGNAL(sendRows(int, int, int, int)),
proxy, SLOT(getRows(int, int, int, int)));
//set up tableview
ui->tableView->setItemDelegate(new QSqlRelationalDelegate(this));
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelectio n);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows );
ui->tableView->setColumnHidden(0,true); // id
ui->tableView->resizeColumnsToContents();
ui->tableView->horizontalHeader()->setStretchLastSection(true);
}
void TestClass::updateTimeStats()
{
float totScore=0;
float totReactTime=0;
float lowScore=FLT_MAX;
int rowLowScore=0;
int rowHighScore =0;
int rowSlowReact =0;
int rowFastReact =0;
float slowReactTime =FLT_MIN;
float highScore=FLT_MIN;
float fastReactTime=FLT_MAX;
float currentScore=0;
float currentReactTime =0;
QString string;
int totRows=proxy->rowCount();
if (totRows >0)
{
for (int r=0; r<totRows; r++)
{
if (ui->printView->isRowHidden(r)) //Test if user deleted row
continue;
//QModelIndex sourceIndex=mapToSource(proxy->index(r,2));
//QModelIndex sourceIndex2=mapToSource(proxy->index(r,3));
//the above 2 lines gives a "mapToSource not declared" error
// #include <QModelIndex> is declared
currentScore=proxy->index(r,2).data(Qt::DisplayRole).toFloat();
currentReactTime=proxy->index(r,3).data(Qt::DisplayRole).toFloat();
if (currentScore < lowScore)
{
lowScore=currentScore;
rowLowScore=r;
}
if (currentScore > highScore)
{
highScore=currentScore;
rowHighScore =r;
}
if (currentReactTime > slowReactTime)
{
slowReactTime=currentReactTime;
rowSlowReact =r;
}
if (currentReactTime<fastReactTime)
{
fastReactTime=currentReactTime;
rowFastReact =r;
}
totTime+=currentTime;
totReactTime+=currentReactTime;
}
startModel->select();
}
myproxymodel.cpp
#include "myproxymodel.h"
MyProxyModel::MyProxyModel (QObject *parent) :
QSortFilterProxyModel(parent)
{
}
MyProxyModel::~MyProxyModel()
{
}
QVariant MyProxyModel::data ( const QModelIndex & index, int role ) const
{
QModelIndex sourceIndex = mapToSource(index);
if (!sourceIndex.isValid())
return QVariant();
if (m_slowReactRow<0)
{
if ( sourceIndex.row() == m_highScore && sourceIndex.column()== 2 && role == Qt::BackgroundRole )
{
return QVariant( Qt::yellow );
}
else if ( sourceIndex.row() == m_lowScore && sourceIndex.column()== 2 && role == Qt::BackgroundRole )
{
return QVariant( Qt::red );
}
else
{
return QSortFilterProxyModel::data( index, role );
}
}
else
{
if ( sourceIndex.row() == m_highScore && sourceIndex.column()== 2 && role == Qt::BackgroundRole )
{
return QVariant( Qt::yellow );
}
else if ( sourceIndex.row() == m_lowScore && sourceIndex.column()== 2 && role == Qt::BackgroundRole )
{
return QVariant( Qt::red );
}
else if (sourceIndex.row()== m_fastReactRow && sourceIndex.column()==3 && role == Qt::BackgroundRole)
{
return QVariant( Qt::yellow );
}
else if ( sourceIndex.row() == m_slowReactRow && sourceIndex.column()== 3 && role == Qt::BackgroundRole )
{
return QVariant( Qt::red );
}
else
{
return QSortFilterProxyModel::data( index, role );
}
}
}
void MyProxyModel::getRows( int rowLow,int rowHigh, int rowSlowReact, int rowFastReact)
{
m_highScore=rowHigh;
m_lowScore=rowLow;
m_slowReactRow= rowSlowReact;
m_fastReactRow= rowFastReact;
}
This is the method I use...when the user deletes a row, the row is hidden in the tableView, then when the tableview is iterated to find the highest and lowest numbers, the hidden row is skipped. Then the identified rows are sent to the proxy to highlight the correct cells.
Is this the correct way or is there a better way? How do I get this to work?
Here is the code for the tableView creation:
testclass.cpp
void TestClass::createTimerTable() // this is called in the constructor of the form
{
testModel= new QSqlRelationalTableModel(this);
testModel->setEditStrategy(QSqlTableModel::OnRowChange);
testModel->setTable("testtable");
testModel->setRelation(2,QSqlRelation("student","id", "LName"));
testModel->setRelation(3,QSqlRelation("testNum", "id","Test"));
testModel->setHeaderData(1,Qt::Horizontal,"Section");
proxy = new MyProxyModel (this);
proxy->setSourceModel(testModel);
ui->tableView->setModel(proxy);
connect(this,SIGNAL(sendRows(int, int, int, int)),
proxy, SLOT(getRows(int, int, int, int)));
//set up tableview
ui->tableView->setItemDelegate(new QSqlRelationalDelegate(this));
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelectio n);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows );
ui->tableView->setColumnHidden(0,true); // id
ui->tableView->resizeColumnsToContents();
ui->tableView->horizontalHeader()->setStretchLastSection(true);
}
void TestClass::updateTimeStats()
{
float totScore=0;
float totReactTime=0;
float lowScore=FLT_MAX;
int rowLowScore=0;
int rowHighScore =0;
int rowSlowReact =0;
int rowFastReact =0;
float slowReactTime =FLT_MIN;
float highScore=FLT_MIN;
float fastReactTime=FLT_MAX;
float currentScore=0;
float currentReactTime =0;
QString string;
int totRows=proxy->rowCount();
if (totRows >0)
{
for (int r=0; r<totRows; r++)
{
if (ui->printView->isRowHidden(r)) //Test if user deleted row
continue;
//QModelIndex sourceIndex=mapToSource(proxy->index(r,2));
//QModelIndex sourceIndex2=mapToSource(proxy->index(r,3));
//the above 2 lines gives a "mapToSource not declared" error
// #include <QModelIndex> is declared
currentScore=proxy->index(r,2).data(Qt::DisplayRole).toFloat();
currentReactTime=proxy->index(r,3).data(Qt::DisplayRole).toFloat();
if (currentScore < lowScore)
{
lowScore=currentScore;
rowLowScore=r;
}
if (currentScore > highScore)
{
highScore=currentScore;
rowHighScore =r;
}
if (currentReactTime > slowReactTime)
{
slowReactTime=currentReactTime;
rowSlowReact =r;
}
if (currentReactTime<fastReactTime)
{
fastReactTime=currentReactTime;
rowFastReact =r;
}
totTime+=currentTime;
totReactTime+=currentReactTime;
}
startModel->select();
}
myproxymodel.cpp
#include "myproxymodel.h"
MyProxyModel::MyProxyModel (QObject *parent) :
QSortFilterProxyModel(parent)
{
}
MyProxyModel::~MyProxyModel()
{
}
QVariant MyProxyModel::data ( const QModelIndex & index, int role ) const
{
QModelIndex sourceIndex = mapToSource(index);
if (!sourceIndex.isValid())
return QVariant();
if (m_slowReactRow<0)
{
if ( sourceIndex.row() == m_highScore && sourceIndex.column()== 2 && role == Qt::BackgroundRole )
{
return QVariant( Qt::yellow );
}
else if ( sourceIndex.row() == m_lowScore && sourceIndex.column()== 2 && role == Qt::BackgroundRole )
{
return QVariant( Qt::red );
}
else
{
return QSortFilterProxyModel::data( index, role );
}
}
else
{
if ( sourceIndex.row() == m_highScore && sourceIndex.column()== 2 && role == Qt::BackgroundRole )
{
return QVariant( Qt::yellow );
}
else if ( sourceIndex.row() == m_lowScore && sourceIndex.column()== 2 && role == Qt::BackgroundRole )
{
return QVariant( Qt::red );
}
else if (sourceIndex.row()== m_fastReactRow && sourceIndex.column()==3 && role == Qt::BackgroundRole)
{
return QVariant( Qt::yellow );
}
else if ( sourceIndex.row() == m_slowReactRow && sourceIndex.column()== 3 && role == Qt::BackgroundRole )
{
return QVariant( Qt::red );
}
else
{
return QSortFilterProxyModel::data( index, role );
}
}
}
void MyProxyModel::getRows( int rowLow,int rowHigh, int rowSlowReact, int rowFastReact)
{
m_highScore=rowHigh;
m_lowScore=rowLow;
m_slowReactRow= rowSlowReact;
m_fastReactRow= rowFastReact;
}