import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
+import QtQuick.Controls 1.4
ApplicationWindow {
- id: root
+ id: addhumanroot
//width: 200
//height: 200
- Text{
+
+ Label {
id: lblname
anchors.left: parent.left
anchors.verticalCenter: name.verticalCenter
id: name
anchors.left: lblname.right
anchors.top: parent.top
+ placeholderText: qsTr("Kawubke")
+ }
+
+ Label {
+ id: lblfirstname
+ anchors.left: parent.left
+ anchors.verticalCenter: firstname.verticalCenter
+ text: qsTr("Firstname: ")
+ }
+
+ TextField {
+ id: firstname
+ anchors.left: lblname.right
+ anchors.top: name.bottom
placeholderText: qsTr("Karl-Heinz")
}
+ Label {
+ id: lblbirthday
+ anchors.left: parent.left
+ anchors.top: lblfirstname.bottom
+ }
+
+ Calendar {
+ id: birthday
+ anchors.left: lblbirthday.right
+ anchors.top: firstname.bottom
+ weekNumbersVisible: true
+ }
+
+ Button {
+ id:ok
+ onClicked: {
+ addHuman(name.text, firstname.text, birthday.selectedDate)
+ addhumanroot.close()
+ }
+ text: qsTr("OK")
+ anchors.bottom: parent.bottom
+ anchors.right: parent.right
+ visible: true
+ }
+
}
#include "database.h"
#include <QtSql/QSqlQuery>
#include <QDebug>
+#include <QDate>
DataBase::DataBase(QObject *parent) : QObject(parent)
{
}
-QVariant DataBase::addHuman(const QString& name, const QString& firstname, const QDate& birthday)
+void DataBase::addHuman(const QVariant& name, const QVariant& firstname,
+ const QVariant& birthday)
{
+ qDebug()<<"addHuman slot called with: "<<name<<", "<<firstname<<", "<< birthday;
QSqlQuery q;
+ if (!q.prepare(this->sqlAddHuman)) qDebug() << "error while preparing: " << q.lastError();
+ q.addBindValue(name.toString());
+ q.addBindValue(firstname.toString());
+ q.addBindValue(birthday.toDate());
+ if (!q.exec())qDebug()<<"error: "<< q.lastError();
}
-QVariant DataBase::addService(const QString& name, const QUrl& url, const QString& description)
+void DataBase::addService(const QString& name, const QUrl& url,
+ const QString& description)
{
-
+ QSqlQuery q;
+ q.prepare(this->sqlAddService);
+ q.addBindValue(name);
+ q.addBindValue(url);
+ q.addBindValue(description);
+ q.exec();
}
-QVariant DataBase::addUser(const QVariant& humanID, const QVariant& serviceID, const QString& username, const QString& password)
+void DataBase::addUser(const QVariant& humanID, const QVariant& serviceID,
+ const QString& username, const QString& password)
{
-
+ QSqlQuery q;
+ q.prepare(this->sqlAddUser);
+ q.addBindValue(humanID);
+ q.addBindValue(serviceID);
+ q.addBindValue(username);
+ q.addBindValue(password);
+ q.exec();
}
QSqlError DataBase::initDB()
{
QStringList tables = db.tables();
QSqlQuery q;
- if (!tables.contains("human", Qt::CaseSensitivity::CaseInsensitive)){ if (!q.exec(this->sqlCreateHumanTable)) return q.lastError();}
+ if (!tables.contains("human", Qt::CaseSensitivity::CaseInsensitive))
+ { if (!q.exec(this->sqlCreateHumanTable)) return q.lastError();}
else qDebug() << "table human already created";
- if (!tables.contains("service", Qt::CaseSensitivity::CaseInsensitive)){ if (!q.exec(this->sqlCreateServiceTable)) return q.lastError();}
+ if (!tables.contains("service", Qt::CaseSensitivity::CaseInsensitive))
+ { if (!q.exec(this->sqlCreateServiceTable)) return q.lastError();}
else qDebug() << "table service already created";
- if (!tables.contains("user", Qt::CaseSensitivity::CaseInsensitive)){ if (!q.exec(this->sqlCreateUserTable)) return q.lastError();}
+ if (!tables.contains("user", Qt::CaseSensitivity::CaseInsensitive))
+ { if (!q.exec(this->sqlCreateUserTable)) return q.lastError();}
else qDebug() << "table user already created";
return QSqlError();
signals:
public slots:
- QVariant addHuman(const QString& name, const QString& firstname, const QDate& birthday);
- QVariant addService(const QString& name, const QUrl& url, const QString& description);
- QVariant addUser(const QVariant& humanID, const QVariant& serviceID, const QString& username, const QString& password);
+ void addHuman(const QVariant& name, const QVariant& firstname,
+ const QVariant& birthday);
+
+ void addService(const QString& name, const QUrl& url,
+ const QString& description);
+
+ void addUser(const QVariant& humanID, const QVariant& serviceID,
+ const QString& username, const QString& password);
private:
QSqlError initDB();
QSqlDatabase db;
- QLatin1String sqlCreateHumanTable = QLatin1String("create table human(id integer primary key, name varchar, firstname varchar, birthday date)");
- QLatin1String sqlCreateServiceTable = QLatin1String("create table service(id integer primary key, name varchar, url varchar, desription varchar)");
- QLatin1String sqlCreateUserTable = QLatin1String(
- "create table user(id integer primary key, humanid integer not null, serviceid integer not null, username varchar, password varchar,"
+ const QLatin1String sqlCreateHumanTable = QLatin1String(
+ "create table human("
+ "id integer primary key autoincrement,"
+ "name varchar,"
+ "firstname varchar,"
+ "birthday date"
+ ")");
+
+ const QLatin1String sqlCreateServiceTable = QLatin1String(
+ "create table service("
+ "id integer primary key autoincrement,"
+ "name varchar,"
+ "url varchar,"
+ "desription 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))");
+ "foreign key(serviceid) references service(id)"
+ ")");
+
+ const QLatin1String sqlAddHuman = QLatin1String(
+ "insert or ignore into "
+ "human "
+ "(name, firstname, birthday)"
+ " values "
+ "(?,?,?)"
+ );
+
+ const QLatin1String sqlAddService = QLatin1String(
+ "insert or ignore into "
+ "service "
+ "(name, url, description)"
+ " values "
+ "(?,?,?)"
+ );
+
+ const QLatin1String sqlAddUser = QLatin1String(
+ "insert or ignore into "
+ "user "
+ "(humanid, serviceid, username, password)"
+ " values "
+ "(?,?,?,?)"
+ );
};
#endif // DATABASE_H
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "database.h"
+#include <QList>
+#include <QVariant>
int main(int argc, char *argv[])
{
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
DataBase db;
+ QList<QObject*> roots = engine.rootObjects();
+ QObject* addHuman = roots.first();
+ for (QObject* qo : roots) {
+ if (qo->objectName() == "root")
+ addHuman = qo;
+ }
+ if (addHuman != nullptr)
+ QObject::connect(addHuman,SIGNAL(addHuman(QVariant, QVariant, QVariant)),
+ &db, SLOT(addHuman(QVariant, QVariant, QVariant)));
return app.exec();
}
width: 640
height: 480
title: qsTr("Hello World")
+ signal addHuman(var name, var firstname, var birthday)
Button {
id: btnaddhuman