From: John Janus Date: Wed, 1 Mar 2017 22:03:10 +0000 (+0100) Subject: added controller X-Git-Url: https://git.johnzone.org/?a=commitdiff_plain;h=5331f4e408b7f500c29d2240ba30b8135604c282;p=UserSheets.git added controller Does not work yet --- diff --git a/UserSheets.pro b/UserSheets.pro index 9e2b8c6..ac79a87 100644 --- a/UserSheets.pro +++ b/UserSheets.pro @@ -3,7 +3,8 @@ QT += qml quick sql CONFIG += c++11 SOURCES += main.cpp \ - database.cpp + database.cpp \ + controller.cpp RESOURCES += qml.qrc @@ -30,4 +31,5 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target HEADERS += \ - database.h + database.h \ + controller.h diff --git a/AddHuman.qml b/addhuman.qml similarity index 94% rename from AddHuman.qml rename to addhuman.qml index d0b8bef..8c31627 100644 --- a/AddHuman.qml +++ b/addhuman.qml @@ -3,8 +3,9 @@ import QtQuick.Controls 2.0 import QtQuick.Layouts 1.0 import QtQuick.Controls 1.4 -ApplicationWindow { +Popup { id: addhumanroot + signal addHuman(var name, var firstname, var birthday) //width: 200 //height: 200 diff --git a/controller.cpp b/controller.cpp new file mode 100644 index 0000000..be53717 --- /dev/null +++ b/controller.cpp @@ -0,0 +1,29 @@ +#include "controller.h" + +Controller::Controller(QQmlApplicationEngine* e, DataBase* d, QObject *parent) : + QObject(parent), + engine{e}, db{d} +{ + this->engine->load(QUrl(QLatin1String("qrc:/main.qml"))); + //connect qml Signals to controller + QList roots = engine->rootObjects(); + QObject* qmlRoot = roots.first(); + + QObject::connect(qmlRoot,SIGNAL(openAddHuman()), + this, SLOT(openAddHuman())); + +} + + +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))); + + +} diff --git a/controller.h b/controller.h new file mode 100644 index 0000000..090a96e --- /dev/null +++ b/controller.h @@ -0,0 +1,26 @@ +#ifndef CONTROLLER_H +#define CONTROLLER_H +#include +#include +#include "database.h" +#include +#include +#include + +class Controller : public QObject +{ + Q_OBJECT +public: + explicit Controller(QQmlApplicationEngine* engine, DataBase* db, QObject *parent = 0); + +signals: + +public slots: + void openAddHuman(); + +private: + QQmlApplicationEngine* engine; + DataBase* db; +}; + +#endif // CONTROLLER_H diff --git a/main.cpp b/main.cpp index 5b26229..b1c77b6 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,7 @@ #include #include #include "database.h" +#include "controller.h" #include #include @@ -10,18 +11,8 @@ int main(int argc, char *argv[]) QGuiApplication app(argc, argv); QQmlApplicationEngine engine; - engine.load(QUrl(QLatin1String("qrc:/main.qml"))); DataBase db; - - QList 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))); + Controller c(&engine, &db); return app.exec(); } diff --git a/main.qml b/main.qml index b245609..6f91177 100644 --- a/main.qml +++ b/main.qml @@ -8,7 +8,7 @@ ApplicationWindow { width: 640 height: 480 title: qsTr("Hello World") - signal addHuman(var name, var firstname, var birthday) + signal openAddHuman() Button { id: btnaddhuman @@ -16,9 +16,7 @@ ApplicationWindow { visible: true text: "Add Human" onClicked: { - var addHumanDialog = Qt.createComponent("AddHuman.qml") - var window = addHumanDialog.createObject(root) - window.show(); + openAddHuman() } } } diff --git a/qml.qrc b/qml.qrc index 3c1c2b5..5ddf2b4 100644 --- a/qml.qrc +++ b/qml.qrc @@ -2,6 +2,6 @@ main.qml qtquickcontrols2.conf - AddHuman.qml + addhuman.qml diff --git a/qtquickcontrols2.conf b/qtquickcontrols2.conf index 1764b16..9f9367b 100644 --- a/qtquickcontrols2.conf +++ b/qtquickcontrols2.conf @@ -3,7 +3,7 @@ ; http://doc.qt.io/qt-5/qtquickcontrols2-styles.html [Controls] -Style=Default +Style=Universal [Universal] Theme=Light