Qml property custom type without creation



  • Hi,

    I have a c++ class which i want to use in qml and in other c++ classes;

    Say C++ class TimeHandler; and a class UiHandler;

    I register TimeHandler as a qmlType.

    In the class UiHandler I create 2 instances of TimeHandler and call
    @
    setContextProperty("thandler1",&timeHandlerObj);
    setContextProperty("thandler2",&timeHandlerObj);
    @

    now i have a qml component:
    @
    Item{
    property TimeHandler timeHandler;

    //some code which uses timeHandler and calls TimeHandler functions
    }
    @

    this Item I use in two Pages and there I set:

    @
    DateItem{
    timeHandler: thandler1
    }
    @

    and in the other Page thandler2

    but the Problem I have is that the qml-code: property TimeHandler already creates an Object (because if I try to use qmlRegisterUncreatableType I get an error in qml.)

    Is there a way to set those timeHandlers without creating unused Objects ?

    Hope you can understand my question :)



  • Hi,

    A couple of things:
    @
    Item {
    property Rectangle someRectangle: null // shouldn't result in the instantiation of a Rectangle
    }
    @

    So you should be able to just do as you did above, without it constructing an extra one. Just don't initialise the property, or initialise it to null.

    However, there is another way: declare a QtObject property instead of a TimeHandler property, and then register the TimeHandler type as an uncreatable type.

    You'll still be able to access all of the functions and properties from QML, although bindings will be slower since the type information isn't available at compile time.

    Cheers,
    Chris.



  • Thanks I tried to do it with null as default value. But if I register TimeHandler as an UncreatableType it still fails in qml at the property.

    Could you please explain the other way you stated a little bit in more detail? I can't follow.



  • If TimeHandler is registered as an UncreatableType, you cannot use TimeHandler as a property type; you should use QtObject as the property type instead.

    If TimeHandler is registered as a normal creatable type, you can use TimeHandler as a property type, and a property initialisation like "property TimeHandler th: null" should not result in an instance of TimeHandler being created.

    Cheers,
    Chris.



  • Oh I see, now I understand. Thanks a lot for your help.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.