From: John Janus Date: Mon, 6 Mar 2017 07:10:43 +0000 (+0100) Subject: using sql query model, not yet working correctly X-Git-Url: https://git.johnzone.org/?a=commitdiff_plain;h=40f9d7c58efca8696e6c727718a1e33ab3b36005;p=UserSheets.git using sql query model, not yet working correctly --- diff --git a/UserSheets.pro b/UserSheets.pro index a5b4c0e..1cd5e6e 100644 --- a/UserSheets.pro +++ b/UserSheets.pro @@ -1,11 +1,12 @@ -QT += qml quick sql widgets - +QT += qml quick sql +!android: QT += widgets CONFIG += c++11 SOURCES += main.cpp \ database.cpp \ controller.cpp \ - logging.cpp + logging.cpp \ + humandatamodel.cpp RESOURCES += qml.qrc @@ -37,8 +38,10 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin HEADERS += \ database.h \ controller.h \ - logging.h + logging.h \ + humandatamodel.h -DISTFILES += style.astylrc \ - logrules.ini -#DEFINES += QT_LOGGING_CONF="logrules.ini" +DISTFILES += \ + logrules.ini \ + style.astylerc +DEFINES += QT_LOGGING_CONF="logrules.ini" diff --git a/controller.cpp b/controller.cpp index 8455eb9..56767a2 100644 --- a/controller.cpp +++ b/controller.cpp @@ -1,22 +1,29 @@ #include "controller.h" #include #include +#include "humandatamodel.h" Controller::Controller (DataBase* d, QObject* parent) : QObject (parent), db{d} { engine = new QQmlApplicationEngine(); + + qmlRegisterType ("org.johnzone.usersheets.sqlmodels", 1, 0, "HumanDataModel"); + this->engine->load (QUrl (QLatin1String ("qrc:/main.qml") ) ); //connect qml Signals to controller QList roots = engine->rootObjects(); - QObject* qmlRoot = roots.first(); + qmlRoot = roots.first(); QObject::connect (qmlRoot, SIGNAL (openAddHuman() ), this, SLOT (openAddHuman() ) ); QObject::connect (qmlRoot, SIGNAL (openAddService() ), this, SLOT (openAddService() ) ); + QObject* humanslist = qmlRoot->findChild ("humanslist"); + + QObject::connect (db, SIGNAL (humanChanged() ), humanslist, SLOT (dataChanged() ) ); } @@ -31,6 +38,8 @@ void Controller::openAddHuman() QQmlEngine::setObjectOwnership (dialog, QQmlEngine::JavaScriptOwnership); QObject::connect (dialog, SIGNAL (addHuman (QVariant, QVariant, QVariant) ), db, SLOT (addHuman (QVariant, QVariant, QVariant) ) ); +// QObject::connect(db, SIGNAL(humanChanged(), +// qmlRoot, SLOT())); } else { qDebug() << "Dialog not loaded"; diff --git a/controller.h b/controller.h index 7ccf519..a527fbb 100644 --- a/controller.h +++ b/controller.h @@ -23,6 +23,7 @@ public slots: private: QQmlApplicationEngine* engine; DataBase* db; + QObject* qmlRoot; }; #endif // CONTROLLER_H diff --git a/database.cpp b/database.cpp index 7128599..2f88c87 100644 --- a/database.cpp +++ b/database.cpp @@ -8,7 +8,7 @@ DataBase::DataBase (QObject* parent) : QObject (parent) db = QSqlDatabase::addDatabase ("QSQLITE"); db.setDatabaseName ("usersheets.db"); if (!db.open() ) { - //do stuff + qCCritical (dbase) << "Opening database failed."; } qCDebug (dbase) << initDB(); @@ -21,7 +21,7 @@ DataBase::~DataBase() } void DataBase::addHuman (const QVariant& name, const QVariant& firstname, - const QVariant& birthday) + const QVariant& birthday) const { qCDebug (dbase) << "addHuman slot called with: " << name << ", " << firstname << ", " << birthday; QSqlQuery q; @@ -30,11 +30,12 @@ void DataBase::addHuman (const QVariant& name, const QVariant& firstname, q.addBindValue (firstname.toString() ); q.addBindValue (birthday.toDate() ); if (!q.exec() ) qCDebug (dbase) << "error: " << q.lastError(); + else emit humanChanged(); } void DataBase::addService (const QVariant& name, const QVariant& url, - const QVariant& description) + const QVariant& description) const { qCDebug (dbase) << "addService slot called with: " << name << ", " << url << ", " << description; QSqlQuery q; @@ -43,10 +44,11 @@ void DataBase::addService (const QVariant& name, const QVariant& url, q.addBindValue (url.toUrl() ); q.addBindValue (description.toString() ); if (!q.exec() ) qCDebug (dbase) << "error: " << q.lastError(); + else emit serviceChanged(); } void DataBase::addUser (const QVariant& humanID, const QVariant& serviceID, - const QString& username, const QString& password) + const QString& username, const QString& password) const { QSqlQuery q; q.prepare (this->sqlAddUser); @@ -54,10 +56,11 @@ void DataBase::addUser (const QVariant& humanID, const QVariant& serviceID, q.addBindValue (serviceID); q.addBindValue (username); q.addBindValue (password); - q.exec(); + if (!q.exec() ) qCDebug (dbase) << "error: " << q.lastError(); + else emit serviceChanged(); } -QSqlError DataBase::initDB() +QSqlError DataBase::initDB() const { QStringList tables = db.tables(); QSqlQuery q; diff --git a/database.h b/database.h index 677aea0..52e8da3 100644 --- a/database.h +++ b/database.h @@ -11,77 +11,82 @@ class DataBase : public QObject { Q_OBJECT + QSqlError initDB() const; + QSqlDatabase db; + public: explicit DataBase (QObject* parent = 0); virtual ~DataBase(); signals: + void humanChanged() const; + void serviceChanged() const; + void userChanged() const; public slots: void addHuman (const QVariant& name, const QVariant& firstname, - const QVariant& birthday); + const QVariant& birthday) const; void addService (const QVariant& name, const QVariant& url, - const QVariant& description); + const QVariant& description) const; void addUser (const QVariant& humanID, const QVariant& serviceID, - const QString& username, const QString& password); + const QString& username, const QString& password) const; private: - QSqlError initDB(); - QSqlDatabase db; - - const QLatin1String sqlCreateHumanTable = QLatin1String ( - "create table human(" - "id integer primary key autoincrement," - "name varchar not null," - "firstname varchar," - "birthday date" - ")"); + const QLatin1String sqlCreateHumanTable + { + "create table human(" + "id integer primary key autoincrement," + "name varchar not null," + "firstname varchar," + "birthday date" + ")" + }; - const QLatin1String sqlCreateServiceTable = QLatin1String ( - "create table service(" - "id integer primary key autoincrement," - "name varchar not null," - "url varchar not null," - "description varchar" - ")"); + const QLatin1String sqlCreateServiceTable { + "create table service(" + "id integer primary key autoincrement," + "name varchar not null," + "url varchar not null," + "description varchar" + ")"}; - const QLatin1String sqlCreateUserTable = QLatin1String ( - "create table user(" - "id integer primary key autoincrement," - "humanid integer not null," - "serviceid integer not null," - "username varchar," - "password varchar," - "foreign key(humanid) references human(id)," - "foreign key(serviceid) references service(id)" - ")"); + const QLatin1String sqlCreateUserTable { + "create table user(" + "id integer primary key autoincrement," + "humanid integer not null," + "serviceid integer not null," + "username varchar," + "password varchar," + "foreign key(humanid) references human(id)," + "foreign key(serviceid) references service(id)" + ")"}; - const QLatin1String sqlAddHuman = QLatin1String ( - "insert or ignore into " - "human " - "(name, firstname, birthday)" - " values " - "(?,?,?)" - ); + const QLatin1String sqlAddHuman { + "insert or ignore into " + "human " + "(name, firstname, birthday)" + " values " + "(?,?,?)" + }; - const QLatin1String sqlAddService = QLatin1String ( - "insert or ignore into " - "service " - "(name, url, description)" - " values " - "(?,?,?)" - ); + const QLatin1String sqlAddService { + "insert or ignore into " + "service " + "(name, url, description)" + " values " + "(?,?,?)" + }; - const QLatin1String sqlAddUser = QLatin1String ( - "insert or ignore into " - "user " - "(humanid, serviceid, username, password)" - " values " - "(?,?,?,?)" - ); + const QLatin1String sqlAddUser { + "insert or ignore into " + "user " + "(humanid, serviceid, username, password)" + " values " + "(?,?,?,?)" + }; }; #endif // DATABASE_H diff --git a/humandatamodel.cpp b/humandatamodel.cpp new file mode 100644 index 0000000..1879f67 --- /dev/null +++ b/humandatamodel.cpp @@ -0,0 +1,42 @@ +#include "humandatamodel.h" +#include +#include +#include + +HumanDataModel::HumanDataModel (QObject* parent) + : QSqlQueryModel (parent) +{ + QSqlQuery query; + if (!query.exec ("select * from human") ) qCCritical (dbase) << "Query for human failed: " << query.lastError(); + setQuery (query); + if (lastError().isValid() ) qCCritical (dbase) << "Could not set query for HumanDataModel: " << lastError(); + +} + +HumanDataModel::~HumanDataModel() +{ + +} + +QHash HumanDataModel::roleNames() const +{ + QHash roles; + for (int i = 0; i < record().count(); i++) roles.insert (Qt::UserRole + i + 1, record().fieldName (i).toUtf8() ); + return roles; +} + +QVariant HumanDataModel::data (const QModelIndex& index, int role) const +{ + QVariant data; + + if (index.isValid() ) { + if (role < Qt::UserRole) { + data = QSqlQueryModel::data (index, role); + } else { + int columnIdx = role - Qt::UserRole - 1; + QModelIndex modelIndex = this->index (index.row(), columnIdx); + data = QSqlQueryModel::data (modelIndex, Qt::DisplayRole); + } + } + return data; +} diff --git a/humandatamodel.h b/humandatamodel.h new file mode 100644 index 0000000..c86353a --- /dev/null +++ b/humandatamodel.h @@ -0,0 +1,20 @@ +#ifndef HUMANDATAMODEL_H +#define HUMANDATAMODEL_H +#include +#include +#include +#include +#include "logging.h" + + +class HumanDataModel : public QSqlQueryModel +{ +public: + HumanDataModel (QObject* parent = 0); + ~HumanDataModel(); + + QHash roleNames() const; + QVariant data (const QModelIndex& model, int role) const; +}; + +#endif // HUMANDATAMODEL_H diff --git a/logging.h b/logging.h index 25fd4bb..a022884 100644 --- a/logging.h +++ b/logging.h @@ -1,6 +1,7 @@ #ifndef LOGGING_H #define LOGGING_H #include +#include const QtMsgType loglevel = QtMsgType::QtWarningMsg; Q_DECLARE_LOGGING_CATEGORY (dbase) diff --git a/main.cpp b/main.cpp index 4c99656..a6a5303 100644 --- a/main.cpp +++ b/main.cpp @@ -1,13 +1,13 @@ #include #include "database.h" #include "controller.h" -#include "logging.h" int main (int argc, char* argv[]) { // QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication app (argc, argv); - qSetMessagePattern ("[%{time yyyyMMdd h:mm:ss.zzz t}] %{if-category}%{category}: %{endif}%{message}"); + qSetMessagePattern ("[%{time yyyyMMddThh:mm:ss.zzz t}] [%{type}] " + "[%{file}-%{line}] %{if-category}%{category}: %{endif}%{message}"); DataBase db; Controller c (&db); diff --git a/main.qml b/main.qml index 50af0d4..e14dba3 100644 --- a/main.qml +++ b/main.qml @@ -1,6 +1,9 @@ import QtQuick 2.7 import QtQuick.Controls 1.4 import QtQuick.Layouts 1.0 +import QtQml 2.2 + +import org.johnzone.usersheets.sqlmodels 1.0 ApplicationWindow { id: root @@ -11,6 +14,21 @@ ApplicationWindow { signal openAddHuman() signal openAddService() + ListView { + id: humanslist + objectName: "humanslist" + anchors.left: parent.left + anchors.top:parent.top + anchors.bottom: btnaddhuman.top +// var locale = Qt.locale + model: HumanDataModel {} + delegate: Label { + color: model.id % 2 == 0 ? "red" : "yellow" +// var bd = Date.fromLocaleTimeString(locale, "yyy-MM-dd") + text: model.id + ": " + model.name + ", " + model.firstname + " (" + model.birthday + ")" + } + } + Button { id: btnaddhuman anchors.left: parent.left