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. Access read-only C++ property from QML
QtWS25 Last Chance

Access read-only C++ property from QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
propertiesc++ qml
2 Posts 2 Posters 553 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.
  • T Offline
    T Offline
    Tom asso
    wrote on 1 Nov 2023, 22:14 last edited by Tom asso 11 Jan 2023, 22:43
    #1

    I have defined a C++ class SharedConstants, which contains a Q_PROPERTY constant QString value. I want to read this property but not write it from QML.

    Here is my C++ class:

    /// Constants shared between C++ and QML code
    class SharedConstants : public QObject
    {
        Q_OBJECT
    
    public:
        
      /// Define read-only QString property called "testString"
      Q_PROPERTY(QString testString READ getTestString)
    
      /// Just return a dummy string for now...
      QString getTestString() const {
          std::cout << "**** getTestString()\n";
          return QString("Dummy value");
      }
    };
    

    Here is my QML:

    ApplicationWindow {
        [...]
        property string testString: SharedConstants.testString
    
        Component.onCompleted: {console.log("onCompleted");
                                console.log("testString: ", testString)}
    

    So I expect testString = “Dummy value” in the QML.

    I find that QML can access testString from QML if I set a SharedConstant instance as a context property instead of registering it - i.e. in main.cpp:

    SharedConstants constants;
    engine.rootContext()->setContextProperty("SharedConstants", &constants);
    

    QML output:
    qml: testString: Dummy value

    But I will be adding other stuff to SharedConstants (e.g. enum) so I must actually register SharedConstants (as described here) in main.cpp:

    // Register SharedConstants so QML can access its properties
    qmlRegisterType<SharedConstants>("SharedConstants", 1, 0,
                                     "SharedConstants");
    

    (and I add "import SharedConstants 1.0" to the QML file.)
    But now the QString testString is blank/null when accessed from QML :

    qml: testString:

    Debugging shows that the designated C++ accessor function getTestString() is not invoked either.
    What am I doing wrong here?

    Thanks!

    J 1 Reply Last reply 2 Nov 2023, 01:58
    0
    • T Tom asso
      1 Nov 2023, 22:14

      I have defined a C++ class SharedConstants, which contains a Q_PROPERTY constant QString value. I want to read this property but not write it from QML.

      Here is my C++ class:

      /// Constants shared between C++ and QML code
      class SharedConstants : public QObject
      {
          Q_OBJECT
      
      public:
          
        /// Define read-only QString property called "testString"
        Q_PROPERTY(QString testString READ getTestString)
      
        /// Just return a dummy string for now...
        QString getTestString() const {
            std::cout << "**** getTestString()\n";
            return QString("Dummy value");
        }
      };
      

      Here is my QML:

      ApplicationWindow {
          [...]
          property string testString: SharedConstants.testString
      
          Component.onCompleted: {console.log("onCompleted");
                                  console.log("testString: ", testString)}
      

      So I expect testString = “Dummy value” in the QML.

      I find that QML can access testString from QML if I set a SharedConstant instance as a context property instead of registering it - i.e. in main.cpp:

      SharedConstants constants;
      engine.rootContext()->setContextProperty("SharedConstants", &constants);
      

      QML output:
      qml: testString: Dummy value

      But I will be adding other stuff to SharedConstants (e.g. enum) so I must actually register SharedConstants (as described here) in main.cpp:

      // Register SharedConstants so QML can access its properties
      qmlRegisterType<SharedConstants>("SharedConstants", 1, 0,
                                       "SharedConstants");
      

      (and I add "import SharedConstants 1.0" to the QML file.)
      But now the QString testString is blank/null when accessed from QML :

      qml: testString:

      Debugging shows that the designated C++ accessor function getTestString() is not invoked either.
      What am I doing wrong here?

      Thanks!

      J Offline
      J Offline
      jeremy_k
      wrote on 2 Nov 2023, 01:58 last edited by
      #2

      @Tom-asso said in Access read-only C++ property from QML:

      Here is my QML:

      ApplicationWindow {
          [...]
          property string testString: SharedConstants.testString
      
          Component.onCompleted: {console.log("onCompleted");
                                  console.log("testString: ", testString)}
      

      But I will be adding other stuff to SharedConstants (e.g. enum) so I must actually register SharedConstants (as described here) in main.cpp:

      // Register SharedConstants so QML can access its properties
      qmlRegisterType<SharedConstants>("SharedConstants", 1, 0,
                                       "SharedConstants");
      

      Debugging shows that the designated C++ accessor function getTestString() is not invoked either.
      What am I doing wrong here?

      qmlRegisterType registers an instantiable type, but the QML code above attempts to use it as a singleton. Either instantiate an instance of the type in QML, or use one of the singleton registration methods such as QML_SINGLETON, qmlRegisterSingletonInstance, or qmlRegisterSingletonType.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      1

      1/2

      1 Nov 2023, 22:14

      • Login

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