Hmm, it seems I've forgot a little about Qt models, here is my code, first header file:
#ifndef UEPEOPLEMODEL_H
#define UEPEOPLEMODEL_H
#include <QImage>
#include <QVariant>
#include <QStringList>
#include <QDebug>
#include <QHash>
#include <QByteArray>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQueryModel>
#include <QtSql/QSqlRecord>
#include <QModelIndex>
#include <QQuickImageProvider>
#include "../settings/uedefaults.h"
{
Q_OBJECT
/*
private:
QHash<int, QByteArray> m_ueRoleNames;
void ueGenerateRoleNames();
*/
public:
~UePeopleModel();
int role) const Q_DECL_OVERRIDE;
// void ueRefresh();
/*
inline QHash<int, QByteArray> roleNames() const
{ return this->m_ueRoleNames; }
*/
public:
static const int UePersonNameRole=Qt::UserRole+1;
static const int UePersonImageRole=UePersonNameRole+1;
};
#endif // UEPEOPLEMODEL_H
#ifndef UEPEOPLEMODEL_H
#define UEPEOPLEMODEL_H
#include <QImage>
#include <QVariant>
#include <QStringList>
#include <QDebug>
#include <QHash>
#include <QByteArray>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQueryModel>
#include <QtSql/QSqlRecord>
#include <QModelIndex>
#include <QQuickImageProvider>
#include "../settings/uedefaults.h"
class UePeopleModel : public QSqlQueryModel, QQuickImageProvider
{
Q_OBJECT
/*
private:
QHash<int, QByteArray> m_ueRoleNames;
void ueGenerateRoleNames();
*/
public:
UePeopleModel(QObject *parent=0);
~UePeopleModel();
QVariant data(const QModelIndex &index,
int role) const Q_DECL_OVERRIDE;
// void ueRefresh();
/*
inline QHash<int, QByteArray> roleNames() const
{ return this->m_ueRoleNames; }
*/
public:
static const int UePersonNameRole=Qt::UserRole+1;
static const int UePersonImageRole=UePersonNameRole+1;
};
#endif // UEPEOPLEMODEL_H
To copy to clipboard, switch view to plain text mode
and its implementation:
#include "uepeoplemodel.h"
UePeopleModel
::UePeopleModel(QObject* parent
) QQuickImageProvider(QQmlImageProviderBase::Image,
0)
{
if(!QSqlDatabase::connectionNames().
contains(UePosDatabase
::UeDatabaseConnectionNames::DATABASE_CONNECTION_NAME_PEOPLE,
Qt::CaseInsensitive))
{
db
=QSqlDatabase::addDatabase(UePosDatabase
::DATABASE_DRIVER,
UePosDatabase::UeDatabaseConnectionNames::DATABASE_CONNECTION_NAME_PEOPLE);
} // if
db.setHostName(/*this->uePosSettings()->ueDbHostname()*/UePosDatabase::UeDatabaseConnectionParameters::DATABASE_HOSTNAME);
db.setDatabaseName(/*this->uePosSettings()->ueDbName()*/UePosDatabase::UeDatabaseConnectionParameters::DATABASE_NAME);
db.setUserName(/*this->uePosSettings()->ueDbUser()*/UePosDatabase::UeDatabaseConnectionParameters::DATABASE_USERNAME);
db.setPassword(/*this->uePosSettings()->ueDbPassword()*/UePosDatabase::UeDatabaseConnectionParameters::DATABASE_PASSWORD);
if(db.open())
{
this->setQuery(UePosDatabase::UeSqlQueries::UeTablePeople::SQL_QUERY_GET_ALL_PEOPLE,
db);
//this->ueGenerateRoleNames();
}
else
{
qDebug() << db.lastError().text();
}
} // default constructor
UePeopleModel::~UePeopleModel()
{
} // default destructor
int role) const
{
// QVariant value;
// if(role<Qt::UserRole)
// {
// value=QSqlQueryModel::data(index,
// role);
// }
// else
// {
// int iColumnIndex=role-Qt::UserRole-1;
// QModelIndex modelIndex=this->index(index.row(),
// iColumnIndex);
// value=QSqlQueryModel::data(modelIndex,
// Qt::DisplayRole);
// } // if
// return value;
role);
if(value.isValid()&&role==Qt::DisplayRole)
{
switch(index.column())
{
case UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_ID:
return value.toInt();
case UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_NAME:
return value.toString();
case UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_APPPASSWORD:
return value.toString();
case UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_CARD:
return value.toString();
case UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_IMAGE:
{
image.loadFromData(value.toByteArray());
return image;
} // case
default:
return value;
} // switch
} // if
} // data
/*
void UePeopleModel::ueRefresh()
{
this->setQuery(UePosDatabase::UeSqlQueries::UeTablePeople::SQL_QUERY_GET_ALL_PEOPLE);
} // ueRefresh
*/
//void UePeopleModel::ueGenerateRoleNames()
//{
// //this->roleNames().clear();
// m_ueRoleNames.clear();
// for(int iIndex=0; iIndex<this->record().count(); iIndex++)
// {
///*
// this->roleNames().insert(Qt::UserRole+1+iIndex,
// this->record().fieldName(iIndex).toUtf8());
//*/
// m_ueRoleNames.insert(Qt::UserRole+1+iIndex,
// this->record().fieldName(iIndex).toUtf8());
// } // for
//} // ueGenerateRoleNames
#include "uepeoplemodel.h"
UePeopleModel::UePeopleModel(QObject* parent)
: QSqlQueryModel(parent),
QQuickImageProvider(QQmlImageProviderBase::Image,
0)
{
QSqlDatabase db;
if(!QSqlDatabase::connectionNames().contains(UePosDatabase::UeDatabaseConnectionNames::DATABASE_CONNECTION_NAME_PEOPLE,
Qt::CaseInsensitive))
{
db=QSqlDatabase::addDatabase(UePosDatabase::DATABASE_DRIVER,
UePosDatabase::UeDatabaseConnectionNames::DATABASE_CONNECTION_NAME_PEOPLE);
} // if
db.setHostName(/*this->uePosSettings()->ueDbHostname()*/UePosDatabase::UeDatabaseConnectionParameters::DATABASE_HOSTNAME);
db.setDatabaseName(/*this->uePosSettings()->ueDbName()*/UePosDatabase::UeDatabaseConnectionParameters::DATABASE_NAME);
db.setUserName(/*this->uePosSettings()->ueDbUser()*/UePosDatabase::UeDatabaseConnectionParameters::DATABASE_USERNAME);
db.setPassword(/*this->uePosSettings()->ueDbPassword()*/UePosDatabase::UeDatabaseConnectionParameters::DATABASE_PASSWORD);
if(db.open())
{
this->setQuery(UePosDatabase::UeSqlQueries::UeTablePeople::SQL_QUERY_GET_ALL_PEOPLE,
db);
//this->ueGenerateRoleNames();
}
else
{
qDebug() << db.lastError().text();
}
} // default constructor
UePeopleModel::~UePeopleModel()
{
} // default destructor
QVariant UePeopleModel::data(const QModelIndex &index,
int role) const
{
// QVariant value;
// if(role<Qt::UserRole)
// {
// value=QSqlQueryModel::data(index,
// role);
// }
// else
// {
// int iColumnIndex=role-Qt::UserRole-1;
// QModelIndex modelIndex=this->index(index.row(),
// iColumnIndex);
// value=QSqlQueryModel::data(modelIndex,
// Qt::DisplayRole);
// } // if
// return value;
QVariant value=QSqlQueryModel::data(index,
role);
if(value.isValid()&&role==Qt::DisplayRole)
{
switch(index.column())
{
case UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_ID:
return value.toInt();
case UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_NAME:
return value.toString();
case UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_APPPASSWORD:
return value.toString();
case UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_CARD:
return value.toString();
case UePosDatabase::UeTableIndexes::UeTablePeople::INDEX_IMAGE:
{
QImage image;
image.loadFromData(value.toByteArray());
return image;
} // case
default:
return value;
} // switch
} // if
return QVariant();
} // data
/*
void UePeopleModel::ueRefresh()
{
this->setQuery(UePosDatabase::UeSqlQueries::UeTablePeople::SQL_QUERY_GET_ALL_PEOPLE);
} // ueRefresh
*/
//void UePeopleModel::ueGenerateRoleNames()
//{
// //this->roleNames().clear();
// m_ueRoleNames.clear();
// for(int iIndex=0; iIndex<this->record().count(); iIndex++)
// {
///*
// this->roleNames().insert(Qt::UserRole+1+iIndex,
// this->record().fieldName(iIndex).toUtf8());
//*/
// m_ueRoleNames.insert(Qt::UserRole+1+iIndex,
// this->record().fieldName(iIndex).toUtf8());
// } // for
//} // ueGenerateRoleNames
To copy to clipboard, switch view to plain text mode
Am I using Qt:isplayRole in right way or should I implement custom roles? And how do I then use this data()method in qml?
Bookmarks