Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. MVVM example with QT QML + C++
Forum Updated to NodeBB v4.3 + New Features

MVVM example with QT QML + C++

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 4.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AROH
    wrote on last edited by
    #1

    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

    dheerendraD 1 Reply Last reply
    0
    • A AROH

      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

      dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by dheerendra
      #2

      MainModel cannot be exposed using to QML.
      Do the following in main.

      MainModel mainMod;
      MainViewModel mainModell(mainMod);

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      A 1 Reply Last reply
      0
      • dheerendraD dheerendra

        MainModel cannot be exposed using to QML.
        Do the following in main.

        MainModel mainMod;
        MainViewModel mainModell(mainMod);

        A Offline
        A Offline
        AROH
        wrote on last edited by
        #3

        @dheerendra

        Thanks for the input, it worked for me.

        1 Reply Last reply
        0
        • A AROH has marked this topic as solved on

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved