[Solved] Making a QML plugin for Sql Databases that makes models.
-
I started by using a tutorial "on-line":http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML and noticed that it is a little old and so I looked at examples. And Still can not get it to work.
UPDATE
The Code is now working at the repo, Thanks to all for your Help.
It turned out as stated in a post below that I needed to change the generateRoleNames to just roleNames. Feel Free to try out the Code yourself at the Link below. There is a example file and docs in the repo located here.
https://github.com/bobweaver/sqlDatabase -
Hi,
Your SqlModel type (I assume it extends QAbstractListModel?) needs to implement the following virtual function:
QHash<int,QByteArray> roleNames() const;
eg: a static example, with predefined rolenames:
@
class SqlModel : public QAbstractListModel
{
Q_OBJECT
Q_ENUMS(Roles)public:
enum Roles {
EmployeeIdentifier = 0,
EmployeeName,
EmployeeImage
};SqlModel(QObject *parent); ~SqlModel(); int rowCount(const QModelIndex &parent) const; QVariant data(const QModelIndex &index, int role) const; QHash<int,QByteArray> roleNames() const;
};
// ... the other functions / ctor / dtor definitions go here...
QHash<int,QByteArray> SqlModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles.insert(EmployeeIdentifier, "employeeIdentifier");
roles.insert(EmployeeName, "employeeName");
roles.insert(EmployeeImage, "employeeImage");
return roles;
}
@ -
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
@ -
Bump ?
-
I know nothing about QSqlQueryModel unfortunately. Whenever I've exposed SQL data to QML, I've done it via a QAbstractListModel-derived type, and then in the implementation, manually performed queries on the SQL database via QtSql classes.
But, where is your roleNames() implementation? You have a generateRoleNames() function but you don't seem to implement roleNames()?
Cheers,
Chris.