Hi,

I want to create a model that not only displays the data from an QSqlQuery but also changes the fields background-color. The color is passed by another query.

I tried to do that by subclassing QAbstractTableModel.

Here is my Code

Qt Code:
  1. #ifndef MDL_STATUS_H
  2. #define MDL_STATUS_H
  3.  
  4. #include <QAbstractTableModel>
  5. #include <QSqlQuery>
  6.  
  7. class QStatusTableModel : public QAbstractTableModel {
  8. Q_OBJECT
  9. public:
  10. QStatusTableModel(const QSqlQuery &qry=QSqlQuery(), QObject *parent = 0)
  11. : QAbstractTableModel(parent), query(qry) {}
  12. int rowCount(const QModelIndex &parent = QModelIndex()) const;
  13. int columnCount(const QModelIndex &parent = QModelIndex()) const;
  14. QVariant data(const QModelIndex &index, int role) const;
  15. QVariant headerData(int section, Qt::Orientation orientation,
  16. int role = Qt::DisplayRole) const;
  17. void setQuery(const QSqlQuery &query);
  18. private:
  19. QSqlQuery query;
  20. };
  21. #endif
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #include "mdl_status.h"
  2. #include <QSqlRecord>
  3.  
  4. int QStatusTableModel::rowCount(const QModelIndex &parent) const {
  5. return query.size();
  6. }
  7.  
  8. int QStatusTableModel::columnCount(const QModelIndex &parent) const {
  9. if (query.isValid() && query.size() > 0) {
  10. return query.record().count();
  11. }
  12. return 0;
  13. }
  14.  
  15. QVariant QStatusTableModel::data(const QModelIndex &index, int role) const {
  16. if (!index.isValid())
  17. return QVariant();
  18. if (index.row() >= query.size())
  19. return QVariant();
  20. if (role == Qt::DisplayRole) {
  21. query.seek(index.row());
  22. return query.value(index.column());
  23. } else {
  24. return QVariant();
  25. }
  26. }
  27.  
  28. QVariant QStatusTableModel::headerData(int section, Qt::Orientation orientation, int role) const {
  29. if (role != Qt::DisplayRole)
  30. return QVariant();
  31. if (orientation == Qt::Horizontal)
  32. return QString("Column %1").arg(section);
  33. else
  34. return QString("Row %1").arg(section);
  35. }
  36.  
  37. void QStatusTableModel::setQuery(const QSqlQuery &qry) {
  38. query = qry;
  39. }
To copy to clipboard, switch view to plain text mode 

If I try to compile this code I get the Error:
error: passing `const QSqlQuery' as `this' argument of `bool QSqlQuery::seek(int, bool)' discards qualifiers

What do I do wrong? Is there another way to display what I want?

Chris