You will have to subclass QSqlQueryModel and reimplement data() and rowCount() and perhaps some more methods. You can do something like this:
In myquerymodel.h file:
#include <QSqlQueryModel>
{
Q_OBJECT
public:
virtual QVariant data
(const QModelIndex
& item,
int role
= Qt
::DisplayRole) const;
virtual int rowCount
(const QModelIndex
& parent
= QModelIndex()) const;
};
#include <QSqlQueryModel>
class MyQueryModel : public QSqlQueryModel
{
Q_OBJECT
public:
MyQueryModel(QObject *parent = 0);
virtual QVariant data(const QModelIndex& item, int role = Qt::DisplayRole) const;
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
};
To copy to clipboard, switch view to plain text mode
In the myquerymodel.cpp file:
{
}
QVariant MyQueryModel
::data(const QModelIndex
& item,
int role
) const {
if(item.row() == 0)
QModelIndex fakeItem
= this
->QSqlQueryModel
::index(item.
row()-1, item.
column());
return this->QSqlQueryModel::data(fakeItem, role);
}
int MyQueryModel::rowCount(const QModelIndex& parent) const
{
return this->QSqlQueryModel::rowCount(parent) + 1;
}
MyQueryModel::MyQueryModel(QObject *parent) : QSqlQueryModel(parent)
{
}
QVariant MyQueryModel::data(const QModelIndex& item, int role) const
{
if(item.row() == 0)
return QVariant();
QModelIndex fakeItem = this->QSqlQueryModel::index(item.row()-1, item.column());
return this->QSqlQueryModel::data(fakeItem, role);
}
int MyQueryModel::rowCount(const QModelIndex& parent) const
{
return this->QSqlQueryModel::rowCount(parent) + 1;
}
To copy to clipboard, switch view to plain text mode
Bookmarks