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. set a property to a const QObject pointer C++ / QML
Forum Updated to NodeBB v4.3 + New Features

set a property to a const QObject pointer C++ / QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
registertypeconst qobjectc++ qml
1 Posts 1 Posters 583 Views 3 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
    EmilieG
    wrote on 23 Jun 2020, 09:35 last edited by EmilieG 7 Feb 2020, 14:59
    #1

    Hello,

    I'm trying to access a subclass element as a const pointer from QML (as I know I don't want to modify it from QML, just access const functions.. ). It seems to me that it should be a pretty normal workflow, but I can't find any working example and I can't get it to work.

    Here is a small example.

    main.cpp

        qmlRegisterType<MainClass>("Test", 1, 0, "MainClass");
        qmlRegisterUncreatableType<SubClass>( "Test", 1, 0, "Subclass", QStringLiteral("SubClass should not be created in QML")
        );
    

    MainClass.hpp

    MainClass {
        Q_OBJECT
        Q_PROPERTY( const SubClass* sub_element READ sub_element CONSTANT );
    public:
        const SubClass* sub_element() const { return &m_sub_element; }
    private:
       SubClass m_sub_element;
    }
    

    QML:

    import Test 1.0
    Rectangle {
        property MainClass element
        property SubClass sub_element : element.sub_element 
    } 
    

    Here everything is compiling. But when I run the code and try to call a function from sub_element I have the following error : ReferenceError: sub_element is not defined. If I remove the const everything is working fine, but I would rather not do that.

    If I remove the const from the Q_PROPERTY definition I obviously get a compilation error as I'm trying to convert a const element to a non const one.
    Changing the way I'm registering the SubClass doesn't seem to change anything (I've tried with/without const).

    I know I can go with a non const pointer but I would really like to understand why I should have such a limitation..

    1 Reply Last reply
    0

    1/1

    23 Jun 2020, 09:35

    • Login

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