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
#ifndef MDL_STATUS_H
#define MDL_STATUS_H
#include <QAbstractTableModel>
#include <QSqlQuery>
Q_OBJECT
public:
QVariant headerData
(int section, Qt
::Orientation orientation,
int role = Qt::DisplayRole) const;
private:
};
#endif
#ifndef MDL_STATUS_H
#define MDL_STATUS_H
#include <QAbstractTableModel>
#include <QSqlQuery>
class QStatusTableModel : public QAbstractTableModel {
Q_OBJECT
public:
QStatusTableModel(const QSqlQuery &qry=QSqlQuery(), QObject *parent = 0)
: QAbstractTableModel(parent), query(qry) {}
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const;
void setQuery(const QSqlQuery &query);
private:
QSqlQuery query;
};
#endif
To copy to clipboard, switch view to plain text mode
#include "mdl_status.h"
#include <QSqlRecord>
int QStatusTableModel
::rowCount(const QModelIndex &parent
) const { return query.size();
}
int QStatusTableModel
::columnCount(const QModelIndex &parent
) const { if (query.isValid() && query.size() > 0) {
return query.record().count();
}
return 0;
}
if (!index.isValid())
if (index.row() >= query.size())
if (role == Qt::DisplayRole) {
query.seek(index.row());
return query.value(index.column());
} else {
}
}
QVariant QStatusTableModel
::headerData(int section, Qt
::Orientation orientation,
int role
) const { if (role != Qt::DisplayRole)
if (orientation == Qt::Horizontal)
return QString("Column %1").
arg(section
);
else
return QString("Row %1").
arg(section
);
}
void QStatusTableModel
::setQuery(const QSqlQuery &qry
) { query = qry;
}
#include "mdl_status.h"
#include <QSqlRecord>
int QStatusTableModel::rowCount(const QModelIndex &parent) const {
return query.size();
}
int QStatusTableModel::columnCount(const QModelIndex &parent) const {
if (query.isValid() && query.size() > 0) {
return query.record().count();
}
return 0;
}
QVariant QStatusTableModel::data(const QModelIndex &index, int role) const {
if (!index.isValid())
return QVariant();
if (index.row() >= query.size())
return QVariant();
if (role == Qt::DisplayRole) {
query.seek(index.row());
return query.value(index.column());
} else {
return QVariant();
}
}
QVariant QStatusTableModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal)
return QString("Column %1").arg(section);
else
return QString("Row %1").arg(section);
}
void QStatusTableModel::setQuery(const QSqlQuery &qry) {
query = qry;
}
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
Bookmarks