From 70abf7178c039fa9925a0463db7ca82e2b69f6df Mon Sep 17 00:00:00 2001 From: John Janus Date: Thu, 2 Mar 2017 16:02:40 +0100 Subject: [PATCH] adds addService and fixes dialogs - switched to Quick.Controls 1.4 to get better desktop integration - added widgets to qt for the same reason --- UserSheets.pro | 2 +- addhuman.qml | 23 ++++++------------- addservice.qml | 53 +++++++++++++++++++++++++++++++++++++++++++ controller.cpp | 47 ++++++++++++++++++++++++++++++-------- controller.h | 4 +++- database.cpp | 15 ++++++------ database.h | 12 +++++----- main.cpp | 9 ++++---- main.qml | 20 +++++++++++----- qml.qrc | 1 + qtquickcontrols2.conf | 6 ++--- 11 files changed, 138 insertions(+), 54 deletions(-) create mode 100644 addservice.qml diff --git a/UserSheets.pro b/UserSheets.pro index ac79a87..8e50682 100644 --- a/UserSheets.pro +++ b/UserSheets.pro @@ -1,4 +1,4 @@ -QT += qml quick sql +QT += qml quick sql widgets CONFIG += c++11 diff --git a/addhuman.qml b/addhuman.qml index 8c31627..5bdc884 100644 --- a/addhuman.qml +++ b/addhuman.qml @@ -1,10 +1,13 @@ import QtQuick 2.7 -import QtQuick.Controls 2.0 +//import QtQuick.Controls 2.0 import QtQuick.Layouts 1.0 import QtQuick.Controls 1.4 +import QtQuick.Dialogs 1.2 -Popup { +Dialog { id: addhumanroot + visible: true + modality: Qt.ApplicationModal signal addHuman(var name, var firstname, var birthday) //width: 200 //height: 200 @@ -32,7 +35,7 @@ Popup { TextField { id: firstname - anchors.left: lblname.right + anchors.left: lblfirstname.right anchors.top: name.bottom placeholderText: qsTr("Karl-Heinz") } @@ -49,17 +52,5 @@ Popup { 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 - } - + onAccepted: addHuman(name.text, firstname.text, birthday.selectedDate) } diff --git a/addservice.qml b/addservice.qml new file mode 100644 index 0000000..4a0941b --- /dev/null +++ b/addservice.qml @@ -0,0 +1,53 @@ +import QtQuick 2.7 +import QtQuick.Layouts 1.0 +import QtQuick.Controls 1.4 +import QtQuick.Dialogs 1.2 + +Dialog { + id: addserviceroot + visible: true + modality: Qt.ApplicationModal + signal addService(var name, var url, var description) + onAccepted: addService(name.text, url.text, description.text) + Label { + id: lblName + anchors.left: parent.left + anchors.verticalCenter: name.verticalCenter + text: qsTr("Name: ") + } + + TextField { + id: name + anchors.left: lblName.right + anchors.top: parent.top + placeholderText: qsTr("service.com") + } + + Label { + id: lblUrl + anchors.left: parent.left + anchors.verticalCenter: url.verticalCenter + text: qsTr("URL: ") + } + + TextField { + id: url + anchors.left: lblUrl.right + anchors.top: name.bottom + placeholderText: qsTr("http://www.service.com") + } + + Label { + id: lblDescription + anchors.left: parent.left + anchors.verticalCenter: description.verticalCenter + text: qsTr("Description: ") + } + + TextField { + id: description + anchors.left: lblDescription.right + anchors.top: url.bottom + placeholderText: qsTr("Description") + } +} diff --git a/controller.cpp b/controller.cpp index be53717..446b74d 100644 --- a/controller.cpp +++ b/controller.cpp @@ -1,9 +1,12 @@ #include "controller.h" +#include +#include -Controller::Controller(QQmlApplicationEngine* e, DataBase* d, QObject *parent) : +Controller::Controller(DataBase* d, QObject *parent) : QObject(parent), - engine{e}, db{d} + db{d} { + engine = new QQmlApplicationEngine(); this->engine->load(QUrl(QLatin1String("qrc:/main.qml"))); //connect qml Signals to controller QList roots = engine->rootObjects(); @@ -12,18 +15,44 @@ Controller::Controller(QQmlApplicationEngine* e, DataBase* d, QObject *parent) : QObject::connect(qmlRoot,SIGNAL(openAddHuman()), this, SLOT(openAddHuman())); + QObject::connect(qmlRoot, SIGNAL(openAddService()), + this, SLOT(openAddService())); + } void Controller::openAddHuman() { -// QQmlContext context(engine, engine->rootObjects().first()); - QQmlComponent component(engine, QUrl(QStringLiteral("qrc:/addHuman.qml"))); -// QQmlContext context = component.creationContext(); - QObject* dialog = component.create(/*&context*/); -// dialog->setParent(engine->rootObjects().first()); -// QObject::connect(dialog, SIGNAL(addHuman(QVariant, QVariant, QVariant)), -// db, SLOT(addHuman(QVariant, QVariant, QVariant))); + QQmlComponent component(engine, QUrl(QStringLiteral("qrc:/addhuman.qml")), QQmlComponent::PreferSynchronous); + + if (component.isReady()) { + QObject* dialog = component.create(); + QQmlEngine::setObjectOwnership(dialog, QQmlEngine::CppOwnership); + dialog->setParent(engine->rootObjects().first()); + QObject::connect(dialog, SIGNAL(addHuman(QVariant, QVariant, QVariant)), + db, SLOT(addHuman(QVariant, QVariant, QVariant))); + + } else { + qDebug() << "Dialog not loaded"; + } +} + +void Controller::openAddService() +{ + QQmlComponent component(engine, QUrl(QStringLiteral("qrc:/addservice.qml")), QQmlComponent::PreferSynchronous); + if (component.isReady()) { + QObject* dialog = component.create(); + QQmlEngine::setObjectOwnership(dialog, QQmlEngine::CppOwnership); + dialog->setParent(engine->rootObjects().first()); + QObject::connect(dialog, SIGNAL(addService(QVariant, QVariant, QVariant)), + db, SLOT(addService(QVariant, QVariant, QVariant))); + } else { + qDebug()<<"Dialog not loaded"; + } +} +Controller::~Controller() +{ + delete engine; } diff --git a/controller.h b/controller.h index 090a96e..029b5a0 100644 --- a/controller.h +++ b/controller.h @@ -11,12 +11,14 @@ class Controller : public QObject { Q_OBJECT public: - explicit Controller(QQmlApplicationEngine* engine, DataBase* db, QObject *parent = 0); + explicit Controller(DataBase* db, QObject *parent = 0); + virtual ~Controller(); signals: public slots: void openAddHuman(); + void openAddService(); private: QQmlApplicationEngine* engine; diff --git a/database.cpp b/database.cpp index be1c6ac..6047d83 100644 --- a/database.cpp +++ b/database.cpp @@ -32,15 +32,16 @@ void DataBase::addHuman(const QVariant& name, const QVariant& firstname, } -void DataBase::addService(const QString& name, const QUrl& url, - const QString& description) +void DataBase::addService(const QVariant& name, const QVariant& url, + const QVariant& description) { + qDebug()<<"addService slot called with: "<sqlAddService); - q.addBindValue(name); - q.addBindValue(url); - q.addBindValue(description); - q.exec(); + if (!q.prepare(this->sqlAddService)) qDebug() << "error while preparing: " << q.lastError(); + q.addBindValue(name.toString()); + q.addBindValue(url.toUrl()); + q.addBindValue(description.toString()); + if (!q.exec())qDebug()<<"error: "<< q.lastError(); } void DataBase::addUser(const QVariant& humanID, const QVariant& serviceID, diff --git a/database.h b/database.h index 56c8081..6f7d604 100644 --- a/database.h +++ b/database.h @@ -20,8 +20,8 @@ public slots: void addHuman(const QVariant& name, const QVariant& firstname, const QVariant& birthday); - void addService(const QString& name, const QUrl& url, - const QString& description); + void addService(const QVariant& name, const QVariant& url, + const QVariant& description); void addUser(const QVariant& humanID, const QVariant& serviceID, const QString& username, const QString& password); @@ -34,7 +34,7 @@ private: const QLatin1String sqlCreateHumanTable = QLatin1String( "create table human(" "id integer primary key autoincrement," - "name varchar," + "name varchar not null," "firstname varchar," "birthday date" ")"); @@ -42,9 +42,9 @@ private: const QLatin1String sqlCreateServiceTable = QLatin1String( "create table service(" "id integer primary key autoincrement," - "name varchar," - "url varchar," - "desription varchar" + "name varchar not null," + "url varchar not null," + "description varchar" ")"); const QLatin1String sqlCreateUserTable = QLatin1String( diff --git a/main.cpp b/main.cpp index b1c77b6..536243b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include "database.h" #include "controller.h" @@ -7,12 +7,11 @@ int main(int argc, char *argv[]) { - QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); - QGuiApplication app(argc, argv); +// QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QApplication app(argc, argv); - QQmlApplicationEngine engine; DataBase db; - Controller c(&engine, &db); + Controller c(&db); return app.exec(); } diff --git a/main.qml b/main.qml index 6f91177..50af0d4 100644 --- a/main.qml +++ b/main.qml @@ -1,5 +1,5 @@ import QtQuick 2.7 -import QtQuick.Controls 2.0 +import QtQuick.Controls 1.4 import QtQuick.Layouts 1.0 ApplicationWindow { @@ -9,14 +9,22 @@ ApplicationWindow { height: 480 title: qsTr("Hello World") signal openAddHuman() + signal openAddService() Button { id: btnaddhuman - anchors.centerIn: parent + anchors.left: parent.left + anchors.bottom: parent.bottom visible: true - text: "Add Human" - onClicked: { - openAddHuman() - } + text: qsTr("Add Human") + onClicked: openAddHuman() + } + + Button { + id: btnaddservice + anchors.left: btnaddhuman.right + anchors.bottom: parent.bottom + text: qsTr("Add Service") + onClicked: openAddService() } } diff --git a/qml.qrc b/qml.qrc index 5ddf2b4..99696fa 100644 --- a/qml.qrc +++ b/qml.qrc @@ -3,5 +3,6 @@ main.qml qtquickcontrols2.conf addhuman.qml + addservice.qml diff --git a/qtquickcontrols2.conf b/qtquickcontrols2.conf index 9f9367b..b01a3e7 100644 --- a/qtquickcontrols2.conf +++ b/qtquickcontrols2.conf @@ -3,13 +3,13 @@ ; http://doc.qt.io/qt-5/qtquickcontrols2-styles.html [Controls] -Style=Universal +Style=Material [Universal] -Theme=Light +Theme=Dark ;Accent=Steel [Material] -Theme=Light +Theme=Dark ;Accent=BlueGrey ;Primary=BlueGray -- 2.47.0