简体   繁体   中英

How set C++ class to an Item in qml?

I have a c++ class with name Class2 and I registered it with qmlRegisterType method and I use it as code behind of qml pages in my application.

I have qml file and in onCompleted I want to get an object of Class2 and set it to cl2 in the qml file .but I get an error

left-hand side of assignment operator is not an lvalue

Rectangle{
    Component.onCompleted: {
        cl2=ObjectAccessor.class2obj;//error is here
    }
    Class2 {
            id: cl2
            onMessageChanged: { }                
    }
}

In qml file I changed the defenition of Class2

property Class2 cl2: Class2 {}

but now i get another error

Cannot assign [undefined] to Class2*

How can i set object of `Class2' so i can all data on this object an all signals?

First of all class2obj being undefined means it is not property interfaced to be accessible from QML.

Second, you cannot assign a qml object to another qml object. It depends on what you want to do.

If you want to assign identity, you will have to put it into a target reference, implemented as a property:

property Class2 cl2: null
...
cl2=ObjectAccessor.class2obj

If you want to assign value, as in to make the property values of the one object to the other, you will have to that member by member.

And if all you need is to connect to a signal, then all you need is a connection element, provided that ObjectAccessor.class2obj actually works:

Connections {
  target: ObjectAccessor.class2obj
  onMessageChanged: doStuff()
}

For a more complete answer, you will have to post the code related to Class2 and ObjectAccessor . And also clarify that bit:

so i can all data on this object an all signals

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM