From 60ee42f734a6775e8851375c19911da9582e91e2 Mon Sep 17 00:00:00 2001 From: John Janus Date: Sat, 5 Aug 2017 02:31:59 +0200 Subject: [PATCH] connection between c++ and qml works. - reworked slots to use normal datatypes again - added dialogs to add customers and services - use native style --- CleaningService.pro | 1 + MainForm.ui.qml | 29 ------------------- dbconn.cpp | 62 +++++++++++++++++++++++++++++++++++------ dbconn.h | 51 +++++++++++++++++++++++++++++---- dialogs/AddCustomer.qml | 30 ++++++++++++++++++++ dialogs/AddService.qml | 33 ++++++++++++++++++++++ main.cpp | 19 +++++++++---- main.qml | 34 ++++++++++++++++------ qml.qrc | 5 +++- 9 files changed, 208 insertions(+), 56 deletions(-) delete mode 100644 MainForm.ui.qml create mode 100644 dialogs/AddCustomer.qml create mode 100644 dialogs/AddService.qml diff --git a/CleaningService.pro b/CleaningService.pro index eeb5607..a450a1d 100644 --- a/CleaningService.pro +++ b/CleaningService.pro @@ -1,6 +1,7 @@ TEMPLATE = app QT += qml quick sql +!android: QT += widgets CONFIG += c++11 SOURCES += main.cpp \ diff --git a/MainForm.ui.qml b/MainForm.ui.qml deleted file mode 100644 index f2cd99e..0000000 --- a/MainForm.ui.qml +++ /dev/null @@ -1,29 +0,0 @@ -import QtQuick 2.7 - -Rectangle { - property alias mouseArea: mouseArea - property alias textEdit: textEdit - - width: 360 - height: 360 - - MouseArea { - id: mouseArea - anchors.fill: parent - } - - TextEdit { - id: textEdit - text: qsTr("Enter some text...") - verticalAlignment: Text.AlignVCenter - anchors.top: parent.top - anchors.horizontalCenter: parent.horizontalCenter - anchors.topMargin: 20 - Rectangle { - anchors.fill: parent - anchors.margins: -10 - color: "transparent" - border.width: 1 - } - } -} diff --git a/dbconn.cpp b/dbconn.cpp index da9e690..9720e29 100644 --- a/dbconn.cpp +++ b/dbconn.cpp @@ -2,6 +2,7 @@ #include "logging.h" #include #include +#include DbConn::DbConn (QObject* parent) : QObject (parent) { @@ -36,20 +37,65 @@ QSqlError DbConn::initDB() const return QSqlError(); } -void DbConn::addService (const QVariant& name, const QVariant& cost) const +void DbConn::addService (const QString& name, const int& cost) const { - + QSqlQuery q; + if (!q.prepare (this->sqlAddService) ) qCCritical (dbase) << "error while preparing: " << q.lastError(); + q.addBindValue (name ); + q.addBindValue (cost ); + if (!q.exec() ) qCCritical (dbase) << "error: " << q.lastError(); + else { + emit servicesChanged(); + qCDebug (dbase) << "added new service " << name << ", costing " << cost; + } } -void DbConn::addCustomer (const QVariant& name, const QVariant& street, const QVariant& number, - const QVariant& city, const QVariant& zip) const +void DbConn::addCustomer (const QString& name, const QString& street, const QString& number, + const QString& city, const uint8_t& zip) const { - + QSqlQuery q; + if (!q.prepare (this->sqlAddCustomer) ) qCCritical (dbase) << "error while preparing: " << q.lastError(); + q.addBindValue (name ); + q.addBindValue (street); + q.addBindValue (number ); + q.addBindValue (city); + q.addBindValue (zip ); + if (!q.exec() ) qCCritical (dbase) << "error: " << q.lastError(); + else { + emit customersChanged(); + qCDebug (dbase) << "added new customer " << name << " in " + << street << " " << number << ", " << zip << " " << city; + } } -void DbConn::addOrder (const QVariant& customerID, QVariant& date, QVariant& items) const +void DbConn::addOrder (const int& customerID, const QDate& date, const QList& items) const { + QSqlQuery q; + if (!q.prepare (this->sqlAddOrder) ) qCCritical (dbase) << "error while preparing: " << q.lastError(); + q.addBindValue (customerID ); + q.addBindValue (date ); + + if (!q.exec() ) qCCritical (dbase) << "error: " << q.lastError(); + else emit ordersChanged(); + QVariant orderID = q.lastInsertId(); + q.clear(); + if (!q.prepare (this->sqlAddOrderItem) ) qCCritical (dbase) << "error while preparing: " << q.lastError(); + QList orderIDList; + for (int i = 0; i < items.length(); i++) { + orderIDList.append (orderID); + } + QVariantList variantItems; + for (int item : items) variantItems.append (item); + q.addBindValue (orderIDList); + q.addBindValue (variantItems); + if (!q.execBatch() ) qCCritical (dbase) << "error: " << q.lastError(); + else emit orderitemsChanged(); } -void DbConn::addInvoice (const QVariant& date, QVariant& OrderID) const +void DbConn::addInvoice (const QDate& date, const int& orderID) const { - + QSqlQuery q; + if (!q.prepare (this->sqlAddInvoice) ) qCCritical (dbase) << "error while preparing: " << q.lastError(); + q.addBindValue (date ); + q.addBindValue (orderID ); + if (!q.exec() ) qCCritical (dbase) << "error: " << q.lastError(); + else emit invoicesChanged(); } diff --git a/dbconn.h b/dbconn.h index d562f6e..8c8065c 100644 --- a/dbconn.h +++ b/dbconn.h @@ -5,6 +5,7 @@ #include #include #include +#include class DbConn : public QObject { @@ -26,6 +27,13 @@ class DbConn : public QObject "zip integer" ")" }; + const QLatin1String sqlAddCustomer { + "insert or ignore into" + " customers " + "(name, street, number, city, zip)" + " values " + "(?,?,?,?,?)" + }; const QLatin1String sqlCreateServicesTable { "create table services(" "id integer primary key autoincrement," @@ -33,6 +41,13 @@ class DbConn : public QObject "costs integer" ")" }; + const QLatin1String sqlAddService { + "insert or ignore into" + " services " + "(name, costs)" + " values " + "(?,?)" + }; const QLatin1String sqlCreateOrdersTable { "create table orders(" "id integer primary key autoincrement," @@ -41,6 +56,13 @@ class DbConn : public QObject "foreign key(customerid) references customers(id)" ")" }; + const QLatin1String sqlAddOrder { + "insert or ignore into" + " orders " + "(customerid, date)" + " values " + "(?,?)" + }; const QLatin1String sqlCreateOrderItemsTable { "create table orderitems(" "id integer primary key autoincrement," @@ -50,6 +72,13 @@ class DbConn : public QObject "foreign key(serviceid) references services(id)" ")" }; + const QLatin1String sqlAddOrderItem { + "insert or ignore into" + " orderitems " + "(orderid, serviceid)" + " values " + "(?,?)" + }; const QLatin1String sqlCreateInvoiceTable { "create table invoices(" "id integer primary key autoincrement," @@ -58,17 +87,29 @@ class DbConn : public QObject "foreign key(orderid) references orders(id)" ")" }; + const QLatin1String sqlAddInvoice { + "insert or ignore into" + " invoices " + "(orderid, date)" + " values " + "(?,?)" + }; public: explicit DbConn (QObject* parent = nullptr); signals: + void servicesChanged() const; + void customersChanged() const; + void ordersChanged() const; + void orderitemsChanged() const; + void invoicesChanged() const; public slots: - void addService (const QVariant& name, const QVariant& cost) const; - void addCustomer (const QVariant& name, const QVariant& street, const QVariant& number, - const QVariant& city, const QVariant& zip) const; - void addOrder (const QVariant& customerID, QVariant& date, QVariant& items) const; - void addInvoice (const QVariant& date, QVariant& OrderID) const; + void addService (const QString& name, const int& cost) const; + void addCustomer (const QString& name, const QString& street, const QString& number, + const QString& city, const uint8_t& zip) const; + void addOrder (const int& customerID, const QDate& date, const QList& items) const; + void addInvoice (const QDate& date, const int& OrderID) const; }; #endif // DBCONN_H diff --git a/dialogs/AddCustomer.qml b/dialogs/AddCustomer.qml new file mode 100644 index 0000000..57a5356 --- /dev/null +++ b/dialogs/AddCustomer.qml @@ -0,0 +1,30 @@ +import QtQuick 2.9 +import QtQuick.Dialogs 1.2 +import QtQuick.Controls 1.4 +import QtQuick.Layouts 1.3 + +Dialog { + id: addcustomer + //visible: true + modality: Qt.WindowModal + title: qsTr("Add Customer") + standardButtons: StandardButton.Save | StandardButton.Cancel + onAccepted: dbconn.addCustomer(nameIn.text, streetIn.text, numberIn.text, cityIn.text, zipIn.text) + GridLayout { + columns: 2 + Label {text: qsTr("Name")} + TextField {id: textIn} + + Label {text: qsTr("Street")} + TextField {id: streetIn} + + Label {text: qsTr("Number")} + TextField {id: numberIn} + + Label {text: qsTr("City")} + TextField {id: cityIn} + + Label {text: qsTr("Zip")} + TextField {id: zipIn; validator: IntValidator {bottom: 0;top: 99999}} + } +} diff --git a/dialogs/AddService.qml b/dialogs/AddService.qml new file mode 100644 index 0000000..f24c3eb --- /dev/null +++ b/dialogs/AddService.qml @@ -0,0 +1,33 @@ +import QtQuick 2.9 +import QtQuick.Dialogs 1.2 +import QtQuick.Controls 1.4 +import QtQuick.Layouts 1.3 + + +Dialog { + id: addservice + //visible: true + modality: Qt.WindowModal + title: qsTr("Add Service") + standardButtons: StandardButton.Save | StandardButton.Cancel + onAccepted: dbconn.addService(nameIn.text, costIn.text) + + GridLayout { + anchors.fill: parent + columns: 2 + Label { + text: qsTr("Name:") + } + TextField { + id: nameIn + } + + Label { + text: qsTr("Cost:") + } + TextField { + id: costIn + validator: IntValidator {bottom: 0} + } + } +} diff --git a/main.cpp b/main.cpp index 82fa785..5116242 100644 --- a/main.cpp +++ b/main.cpp @@ -1,13 +1,22 @@ -#include +#include #include +#include +#include -int main(int argc, char *argv[]) +#include "dbconn.h" + +int main (int argc, char* argv[]) { - QGuiApplication app(argc, argv); + + QApplication app (argc, argv); QQmlApplicationEngine engine; - engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); - if (engine.rootObjects().isEmpty()) + auto root_context = engine.rootContext(); + DbConn db; + root_context->setContextProperty ("dbconn", &db); + + engine.load (QUrl (QStringLiteral ("qrc:/main.qml") ) ); + if (engine.rootObjects().isEmpty() ) return -1; return app.exec(); diff --git a/main.qml b/main.qml index 518666f..725c05e 100644 --- a/main.qml +++ b/main.qml @@ -1,16 +1,34 @@ -import QtQuick 2.7 -import QtQuick.Window 2.2 +import QtQuick 2.9 +import QtQuick.Controls 1.4 -Window { +import "AddBaseData/dialogs" + +ApplicationWindow { + id: root visible: true width: 640 height: 480 - title: qsTr("Hello World") + title: qsTr("Cleaning Service") + AddService { id: addservice } + AddCustomer { id: addcustomer } - MainForm { - anchors.fill: parent - mouseArea.onClicked: { - console.log(qsTr('Clicked on background. Text: "' + textEdit.text + '"')) + menuBar: MenuBar { + Menu { + title: qsTr("&File") + MenuItem { + text: qsTr("Add Service") + onTriggered: addservice.open() + } + MenuItem { + text: qsTr("Add Customer") + onTriggered: addcustomer.open() + } + MenuItem { + text: qsTr("&Quit") + onTriggered: Qt.quit() + } } + } + } diff --git a/qml.qrc b/qml.qrc index 7684346..acb7d01 100644 --- a/qml.qrc +++ b/qml.qrc @@ -1,6 +1,9 @@ main.qml - MainForm.ui.qml + + + dialogs/AddService.qml + dialogs/AddCustomer.qml -- 2.47.0