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. Displaying a C++ class via QML in QtQuick2
Forum Updated to NodeBB v4.3 + New Features

Displaying a C++ class via QML in QtQuick2

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 466 Views 1 Watching
  • 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.
  • E Offline
    E Offline
    emyrcatsor
    wrote on 19 Mar 2022, 10:56 last edited by
    #1

    I am working on a QtQuick 2 application (Qt version 6.2.3). I created one C++ class (let's call this class "Example") that contains the data that my application should deal with. This class can be instantiated several times, representing different datasets to be displayed.

    class ExampleObject : public QObject {
    	Q_OBJECT
    	Q_PROPERTY(QString property1 MEMBER property1 CONSTANT)
    	...
    
    	public:
    	QString property1;
    };
    
    Q_DECLARE_METATYPE(ExampleObject*)
    

    I want to be able to display instances of this class via QML, therefore I created a "Example" custom component with a property pointing to the Example C++ object containing the data I want to display.

    ExampleComponent {
    	property var exampleCppObject // exampleCppObject is a pointer to an instance of ExampleObject
    
    	Label {
    		text: exampleCppObject.property1
    	}
    }
    

    To be able to change the Example instance used by the QML component, I created functions to "reinitialize" and "update" the component:

    ExampleComponent {
    	property var exampleCppObject // exampleCppObject is a pointer to an instance of ExampleObject
    	property string textToDisplay
    
    	function update() {
    		textToDisplay=Qt.binding(() => exampleCppObject.property1);
    	}
    
    	function reinitialize() {
    		textToDisplay=""
    	}
    
    	Label {
    		text: textToDisplay
    	}
    }
    

    I call these functions after changing or deleting the Example object pointed by ExampleCppObject, and this works quite fine. But I feel like this isn't best practice, and it seems to me that I am doing things wrong.

    What are better ways of connecting C++ to QML, in the situation I described?
    Many thanks in advance!

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lemons
      wrote on 21 Mar 2022, 14:54 last edited by
      #2

      @emyrcatsor said in Displaying a C++ class via QML in QtQuick2:

      ExampleObject

      The easiest way is something like this:

      Q_PROPERTY(QString myProperty READ getMyProperty WRITE setMyProperty NOTIFY myPropertyChanged)
      
      public:
          QString getMyProperty();
          void setMyProperty(const QString &myProperty);
      
      signals:
          void myPropertyChanged();
      

      In QML you can access the property by its name. if you update the value on the QML side, it automatically uses the WRITE method to update the c++ object. as long as you emit the NOTIFY signal when updating the objects property, the object will be re-evaluated automatically in QML. (as well as all bindings to this object)


      Also you can use a wrapper class to easily access your objects in QML and pass it as a context property to QML:

      // main.cpp
      WrapperClass wc;
      engine.rootContext()->setContextProperty("WRAPPER",  &wc);
      
      class WrapperClass : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(QList<ExampleObject *> myObjects READ getMyObjects NOTIFY myObjectsChanged)
      public:
          explicit WrapperClass(QObject *parent = nullptr);
      
          QList<ExampleObject *> getMyObjects();
      
      

      then you can use it in QML e.g. like this:

      ListView{
          // WRAPPER is defined as context property in main.cpp
          // myObjects uses the READ method of the the WrapperClass object which returns QList<ExampleObject *>
          model: WRAPPER.myObjects
          delegate: TextField{
              // modelData = model[index] = current object
              // myProperty uses the READ method of your object (ExampleObject) to read its value
              text: modelData.myProperty
              onAccepted: {
                  // update the property using the WRITE method of the object
                  modelData.myProperty = text
              }
          }
      }
      
      1 Reply Last reply
      1
      • E Offline
        E Offline
        emyrcatsor
        wrote on 22 Mar 2022, 15:43 last edited by
        #3

        Thanks so much, that is indeed what I ended with!

        1 Reply Last reply
        0
        • L lemons referenced this topic on 20 Mar 2023, 07:45

        2/3

        21 Mar 2022, 14:54

        • Login

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