MVVM example with QT QML + C++
-
Hello,
I am trying a sample example in MVVM pattern using QT QML + C++, i have drafted the whole example based on the things i read from internet.
But i am unable to execute the same, any help in here will be highly appreciated.MainModel.h
#ifndef MAINMODEL_H #define MAINMODEL_H #include <QObject> class MainModel { private: QString _labelTextString; public: virtual ~MainModel() {} QString getLabelTextString() const {return _labelTextString;} void setLabelTextString(QString &value){ _labelTextString = value; } }; #endif // MAINMODEL_H
MainViewModel.h
#ifndef MAINVIEWMODEL_H #define MAINVIEWMODEL_H #include "mainmodel.h" class MainViewModel : public QObject { Q_OBJECT private: //mainModel MainModel _mainModel; Q_PROPERTY(QString labelText READ labelText WRITE setLabelText NOTIFY labelTextChanged) QString m_labelText = "Happy"; public: explicit MainViewModel(const MainModel& mainModel); virtual ~MainViewModel(){} Q_INVOKABLE void changeText(QString value) { setLabelText(value); } QString labelText() const; public slots: void setLabelText(QString labelText); signals: void labelTextChanged(QString labelText); }; #endif // MAINVIEWMODEL_H
MainViewModel.cpp
#include "mainviewmodel.h" MainViewModel::MainViewModel(const MainModel& mainModel) { _mainModel = mainModel; } QString MainViewModel::labelText() const { return m_labelText; } void MainViewModel::setLabelText(QString labelText) { if (m_labelText == labelText) return; m_labelText = labelText; _mainModel.setLabelTextString(m_labelText); emit labelTextChanged(m_labelText); }
main.qml
import QtQuick 2.9 import QtQuick.Window 2.2 import QtQuick.Controls 2.2 Window { visible: true width: 640 height: 480 title: qsTr("Hello World") Label{ id: sourceTbx x: 201 y: 176 width: 348 height: 27; anchors.verticalCenterOffset: -50 anchors.horizontalCenterOffset: 75 anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter text: model.labelText Binding { target: model; property: "labelText"; value: sourceTbx.text } } Button { x: 244 y: 361 width: 113 height: 37 anchors.verticalCenterOffset: 140 anchors.horizontalCenterOffset: 0 anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter onClicked: model.changeText("Hai") } }
main.cpp
#include <QGuiApplication> #include <QQmlContext> #include <QQmlApplicationEngine> #include "mainviewmodel.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); MainModel mainModell; QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("model", &mainModell); //i am getting error at this place, if i am commenting this then i can see the Window. engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
Thanks in advance !!
Regard's,
Rohith.G -
Hello,
I am trying a sample example in MVVM pattern using QT QML + C++, i have drafted the whole example based on the things i read from internet.
But i am unable to execute the same, any help in here will be highly appreciated.MainModel.h
#ifndef MAINMODEL_H #define MAINMODEL_H #include <QObject> class MainModel { private: QString _labelTextString; public: virtual ~MainModel() {} QString getLabelTextString() const {return _labelTextString;} void setLabelTextString(QString &value){ _labelTextString = value; } }; #endif // MAINMODEL_H
MainViewModel.h
#ifndef MAINVIEWMODEL_H #define MAINVIEWMODEL_H #include "mainmodel.h" class MainViewModel : public QObject { Q_OBJECT private: //mainModel MainModel _mainModel; Q_PROPERTY(QString labelText READ labelText WRITE setLabelText NOTIFY labelTextChanged) QString m_labelText = "Happy"; public: explicit MainViewModel(const MainModel& mainModel); virtual ~MainViewModel(){} Q_INVOKABLE void changeText(QString value) { setLabelText(value); } QString labelText() const; public slots: void setLabelText(QString labelText); signals: void labelTextChanged(QString labelText); }; #endif // MAINVIEWMODEL_H
MainViewModel.cpp
#include "mainviewmodel.h" MainViewModel::MainViewModel(const MainModel& mainModel) { _mainModel = mainModel; } QString MainViewModel::labelText() const { return m_labelText; } void MainViewModel::setLabelText(QString labelText) { if (m_labelText == labelText) return; m_labelText = labelText; _mainModel.setLabelTextString(m_labelText); emit labelTextChanged(m_labelText); }
main.qml
import QtQuick 2.9 import QtQuick.Window 2.2 import QtQuick.Controls 2.2 Window { visible: true width: 640 height: 480 title: qsTr("Hello World") Label{ id: sourceTbx x: 201 y: 176 width: 348 height: 27; anchors.verticalCenterOffset: -50 anchors.horizontalCenterOffset: 75 anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter text: model.labelText Binding { target: model; property: "labelText"; value: sourceTbx.text } } Button { x: 244 y: 361 width: 113 height: 37 anchors.verticalCenterOffset: 140 anchors.horizontalCenterOffset: 0 anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter onClicked: model.changeText("Hai") } }
main.cpp
#include <QGuiApplication> #include <QQmlContext> #include <QQmlApplicationEngine> #include "mainviewmodel.h" int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); MainModel mainModell; QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("model", &mainModell); //i am getting error at this place, if i am commenting this then i can see the Window. engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
Thanks in advance !!
Regard's,
Rohith.GMainModel cannot be exposed using to QML.
Do the following in main.MainModel mainMod;
MainViewModel mainModell(mainMod); -
MainModel cannot be exposed using to QML.
Do the following in main.MainModel mainMod;
MainViewModel mainModell(mainMod);Thanks for the input, it worked for me.
-