void TestClass::createTimerTable() // this is called in the constructor of the form
{
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->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;
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();
}
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::SingleSelection);
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();
}
To copy to clipboard, switch view to plain text mode
myproxymodel.cpp
#include "myproxymodel.h"
MyProxyModel
::MyProxyModel (QObject *parent
) :{
}
MyProxyModel::~MyProxyModel()
{
}
{
if (!sourceIndex.isValid())
if (m_slowReactRow<0)
{
if ( sourceIndex.row() == m_highScore && sourceIndex.column()== 2 && role == Qt::BackgroundRole )
{
}
else if ( sourceIndex.row() == m_lowScore && sourceIndex.column()== 2 && role == Qt::BackgroundRole )
{
}
else
{
}
}
else
{
if ( sourceIndex.row() == m_highScore && sourceIndex.column()== 2 && role == Qt::BackgroundRole )
{
}
else if ( sourceIndex.row() == m_lowScore && sourceIndex.column()== 2 && role == Qt::BackgroundRole )
{
}
else if (sourceIndex.row()== m_fastReactRow && sourceIndex.column()==3 && role == Qt::BackgroundRole)
{
}
else if ( sourceIndex.row() == m_slowReactRow && sourceIndex.column()== 3 && role == Qt::BackgroundRole )
{
}
else
{
}
}
}
void MyProxyModel::getRows( int rowLow,int rowHigh, int rowSlowReact, int rowFastReact)
{
m_highScore=rowHigh;
m_lowScore=rowLow;
m_slowReactRow= rowSlowReact;
m_fastReactRow= rowFastReact;
}
#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;
}
To copy to clipboard, switch view to plain text mode
Bookmarks