CONFIG += c++11
SOURCES += main.cpp \
- database.cpp
+ database.cpp \
+ controller.cpp
RESOURCES += qml.qrc
!isEmpty(target.path): INSTALLS += target
HEADERS += \
- database.h
+ database.h \
+ controller.h
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
--- /dev/null
+#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<QObject*> 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)));
+
+
+}
--- /dev/null
+#ifndef CONTROLLER_H
+#define CONTROLLER_H
+#include <QObject>
+#include <QQmlApplicationEngine>
+#include "database.h"
+#include <QQmlComponent>
+#include <QUrl>
+#include <QQmlContext>
+
+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
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "database.h"
+#include "controller.h"
#include <QList>
#include <QVariant>
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
- 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)));
+ Controller c(&engine, &db);
return app.exec();
}
width: 640
height: 480
title: qsTr("Hello World")
- signal addHuman(var name, var firstname, var birthday)
+ signal openAddHuman()
Button {
id: btnaddhuman
visible: true
text: "Add Human"
onClicked: {
- var addHumanDialog = Qt.createComponent("AddHuman.qml")
- var window = addHumanDialog.createObject(root)
- window.show();
+ openAddHuman()
}
}
}
<qresource prefix="/">
<file>main.qml</file>
<file>qtquickcontrols2.conf</file>
- <file>AddHuman.qml</file>
+ <file>addhuman.qml</file>
</qresource>
</RCC>
; http://doc.qt.io/qt-5/qtquickcontrols2-styles.html
[Controls]
-Style=Default
+Style=Universal
[Universal]
Theme=Light