-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
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"
#include "controller.h"
#include <QDebug>
#include <QQuickWindow>
+#include "humandatamodel.h"
Controller::Controller (DataBase* d, QObject* parent) :
QObject (parent),
db{d}
{
engine = new QQmlApplicationEngine();
+
+ qmlRegisterType<HumanDataModel> ("org.johnzone.usersheets.sqlmodels", 1, 0, "HumanDataModel");
+
this->engine->load (QUrl (QLatin1String ("qrc:/main.qml") ) );
//connect qml Signals to controller
QList<QObject*> 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<QObject*> ("humanslist");
+
+ QObject::connect (db, SIGNAL (humanChanged() ), humanslist, SLOT (dataChanged() ) );
}
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";
private:
QQmlApplicationEngine* engine;
DataBase* db;
+ QObject* qmlRoot;
};
#endif // CONTROLLER_H
db = QSqlDatabase::addDatabase ("QSQLITE");
db.setDatabaseName ("usersheets.db");
if (!db.open() ) {
- //do stuff
+ qCCritical (dbase) << "Opening database failed.";
}
qCDebug (dbase) << initDB();
}
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;
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;
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);
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;
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
--- /dev/null
+#include "humandatamodel.h"
+#include <QSqlQuery>
+#include <QSqlError>
+#include <QSqlRecord>
+
+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<int, QByteArray> HumanDataModel::roleNames() const
+{
+ QHash<int, QByteArray> 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;
+}
--- /dev/null
+#ifndef HUMANDATAMODEL_H
+#define HUMANDATAMODEL_H
+#include <QSqlQueryModel>
+#include <QObject>
+#include <QDebug>
+#include <QHash>
+#include "logging.h"
+
+
+class HumanDataModel : public QSqlQueryModel
+{
+public:
+ HumanDataModel (QObject* parent = 0);
+ ~HumanDataModel();
+
+ QHash<int, QByteArray> roleNames() const;
+ QVariant data (const QModelIndex& model, int role) const;
+};
+
+#endif // HUMANDATAMODEL_H
#ifndef LOGGING_H
#define LOGGING_H
#include <QLoggingCategory>
+#include <QDebug>
const QtMsgType loglevel = QtMsgType::QtWarningMsg;
Q_DECLARE_LOGGING_CATEGORY (dbase)
#include <QApplication>
#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);
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
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