Here is a very simple QAbstractTableModel-based example that will color an entire row based on the value in the cell in a particular column.
// main.cpp
#include "TableTestDlg.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
TableTestDlg w;
w.show();
return a.exec();
}
// main.cpp
#include "TableTestDlg.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TableTestDlg w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
// TableTestDlg.h
#ifndef TABLETEST_H
#define TABLETEST_H
#include <QtWidgets/QDialog>
class TableModel;
class TableTestDlg
: public QDialog{
Q_OBJECT
public:
~TableTestDlg();
private:
TableModel * mpModel;
};
#endif // TABLETEST_H
// TableTestDlg.cpp
#include "TableTestDlg.h"
#include "TableModel.h"
#include <QTableView>
#include <QHBoxLayout>
TableTestDlg
::TableTestDlg(QWidget *parent
){
mpModel = new TableModel( this );
mpView->setModel( mpModel );
pLayout->addWidget( mpView );
setLayout( pLayout );
}
TableTestDlg::~TableTestDlg()
{
}
// TableTestDlg.h
#ifndef TABLETEST_H
#define TABLETEST_H
#include <QtWidgets/QDialog>
class QTableView;
class TableModel;
class TableTestDlg : public QDialog
{
Q_OBJECT
public:
TableTestDlg(QWidget *parent = 0);
~TableTestDlg();
private:
TableModel * mpModel;
QTableView * mpView;
};
#endif // TABLETEST_H
// TableTestDlg.cpp
#include "TableTestDlg.h"
#include "TableModel.h"
#include <QTableView>
#include <QHBoxLayout>
TableTestDlg::TableTestDlg(QWidget *parent)
: QDialog(parent)
{
mpModel = new TableModel( this );
mpView = new QTableView( this );
mpView->setModel( mpModel );
QHBoxLayout * pLayout = new QHBoxLayout( this );
pLayout->addWidget( mpView );
setLayout( pLayout );
}
TableTestDlg::~TableTestDlg()
{
}
To copy to clipboard, switch view to plain text mode
// TableModel.h
#ifndef TABLEMODEL_H
#define TABLEMODEL_H
#include <QAbstractTableModel>
#include <QVector>
{
Q_OBJECT
public:
~TableModel();
private:
QVector< int > mRowData;
};
#endif // TABLEMODEL_H
// TableModel.cpp
#include "TableModel.h"
#include <QBrush>
static int sRowCount = 42;
static int sColumnCount = 4;
static int sNumberColumn = 1;
TableModel
::TableModel(QObject *parent
){
mRowData.resize( sRowCount );
// Set data to an integer - 0, 1, or 2
for ( int nRow = 0; nRow < sRowCount; ++nRow )
mRowData[ nRow ] = int( 3.0 * double( qrand() ) / double( RAND_MAX ) );
}
TableModel::~TableModel()
{
}
int TableModel
::rowCount( const QModelIndex & parent
/*= QModelIndex() */ ) const {
if ( parent.isValid() )
return 0;
return sRowCount;
}
int TableModel
::columnCount( const QModelIndex & parent
/*= QModelIndex() */ ) const {
if ( parent.isValid() )
return 0;
return sColumnCount;
}
{
switch( role )
{
case Qt::DisplayRole:
if ( index.column() == sNumberColumn )
retVal
= QString( "%1" ).
arg( mRowData
[ index.
row() ] );
else
break;
case Qt::BackgroundRole:
if ( mRowData[ index.row() ] > 1 )
else if ( mRowData[ index.row() ] < 1 )
break;
}
return retVal;
}
// TableModel.h
#ifndef TABLEMODEL_H
#define TABLEMODEL_H
#include <QAbstractTableModel>
#include <QVector>
class TableModel : public QAbstractTableModel
{
Q_OBJECT
public:
TableModel(QObject *parent);
~TableModel();
virtual int rowCount( const QModelIndex & parent = QModelIndex() ) const;
virtual int columnCount( const QModelIndex & parent = QModelIndex() ) const;
virtual QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const;
private:
QVector< int > mRowData;
};
#endif // TABLEMODEL_H
// TableModel.cpp
#include "TableModel.h"
#include <QBrush>
static int sRowCount = 42;
static int sColumnCount = 4;
static int sNumberColumn = 1;
TableModel::TableModel(QObject *parent)
: QAbstractTableModel(parent)
{
mRowData.resize( sRowCount );
// Set data to an integer - 0, 1, or 2
for ( int nRow = 0; nRow < sRowCount; ++nRow )
mRowData[ nRow ] = int( 3.0 * double( qrand() ) / double( RAND_MAX ) );
}
TableModel::~TableModel()
{
}
int TableModel::rowCount( const QModelIndex & parent /*= QModelIndex() */ ) const
{
if ( parent.isValid() )
return 0;
return sRowCount;
}
int TableModel::columnCount( const QModelIndex & parent /*= QModelIndex() */ ) const
{
if ( parent.isValid() )
return 0;
return sColumnCount;
}
QVariant TableModel::data( const QModelIndex & index, int role /*= Qt::DisplayRole */ ) const
{
QVariant retVal;
switch( role )
{
case Qt::DisplayRole:
if ( index.column() == sNumberColumn )
retVal = QString( "%1" ).arg( mRowData[ index.row() ] );
else
retVal = QString( "foo" );
break;
case Qt::BackgroundRole:
if ( mRowData[ index.row() ] > 1 )
retVal = QBrush( Qt::red );
else if ( mRowData[ index.row() ] < 1 )
retVal = QBrush( Qt::green );
break;
}
return retVal;
}
To copy to clipboard, switch view to plain text mode
The results look like this TableTest.jpg
Since you are using a QSqlQueryModel (which is also based on QAbstractTableModel) you have two options:
1 - Derive a class from QSqlQueryModel and reimplement the data() method. In the Qt::BackgroundRole case, you probably must make a recursive call to data() with Qt::DisplayRole and the QModelIndexthat corresponds to the column you want to check for the threshold value and convert the result to an int. If the value is greater than the threshold, set the return value to the appropriate QBrush as I show above, otherwise return an empty QVariant. For all other roles, return QSqlQueryModel::data() to make sure your results are displayed as they should be.
2 - Derive a class from QSortFilterProxyModel and use the QSqlQueryModel as the source (QSortFilterProxyModel::setSourceModel()). In your proxy model, implement only the data() method as described in (1) above, except substitute QSortFilterProxyModel::data() for QSqlQueryModel::data().
Bookmarks