This is what I have atm and it is not working thanks for your help
.h
#ifndef SQLQUERYMODEL_H
#define SQLQUERYMODEL_H
#include <QVariant>
#include <QSqlQueryModel>
#include <QSqlDatabase>
class SqlQueryModel : public QSqlQueryModel
{
Q_OBJECT
public:
explicit SqlQueryModel(QObject *parent = 0);
Q_INVOKABLE void setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase());
Q_INVOKABLE void setQuery(const QSqlQuery &query);
Q_INVOKABLE QVariant data(const QModelIndex &index, int role)const;
QHash<int, QByteArray>generateRoleNames() const;
signals:
public slots:
private:
};
#endif // QSQLQUERYMODEL_H
.cpp
#include "sqlquerymodel.h"
#include"sqlquery.h"
#include <QSqlRecord>
#include <QSqlField>
#include <QDebug>
#include <QVariant>
#include <QAbstractListModel>
SqlQueryModel::SqlQueryModel(QObject *parent) :
QSqlQueryModel(parent)
{
}
void SqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db)
{
QSqlQueryModel::setQuery(query,db);
generateRoleNames();
}
void SqlQueryModel::setQuery(const QSqlQuery &query)
{
QSqlQueryModel::setQuery(query);
generateRoleNames();
}
QHash<int, QByteArray>SqlQueryModel::generateRoleNames() const
{
QHash<int,QByteArray> hash;
for( int i = 0; i < record().count(); i++) {
hash.insert(Qt::UserRole + i + 1, QByteArray(record().fieldName(i).toLatin1()));
qDebug() << hash << "\n" ;
}
return hash;
}
QVariant SqlQueryModel::data(const QModelIndex &index, int role)const
{
QVariant value = QSqlQueryModel::data(index, role);
if(role < Qt::UserRole)
{
value = QSqlQueryModel::data(index, role);
}
else
{
int columnIdx = role - Qt::UserRole - 1;
QModelIndex modelIndex = this->index(index.row(), columnIdx);
value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
}
return value;
}
I am qdebuging the hash names as you can see in the function generateRoleNames But sill in qml I Do not get anything back.
from the debug
QHash((268, "title_pronounce")(269, "stereo")(288, "videoprop")(266, "stars")(267, "previouslyshown")(264, "category_type")(265, "airdate")(278, "colorcode")(279, "syndicatedepisodenumber")(276, "originalairdate")(277, "showtype")(274, "parttotal")(275, "seriesid")(272, "closecaptioned")(273, "partnumber")(286, "audioprop")(287, "subtitletypes")(284, "first")(285, "last")(282, "generic")(283, "listingsource")(280, "programid")(281, "manualid")(262, "description")(263, "category")(260, "title")(261, "subtitle")(258, "starttime")(259, "endtime")(257, "chanid")(270, "subtitled")(271, "hdtv"))
getting the count
9874
error saying that the role is not set even though it says it is in qdebug or I dont understand it. kinda new to all this ….
file:///home/joseph/Templates/untitled49/untitled49.qml:20: ReferenceError: last is not defined
file:///home/joseph/Templates/untitled49/untitled49.qml:20: ReferenceError: last is not defined
↧