Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

Access Signal Handlers of Defined Properties



  • Hi,

    I have C++ custom types exposed to QML as D1, D2, D3, etc. All of them inherit from an abstract base class ABC. I want to implement a generic QML component with a property containing any class inheriting from ABC and able to use D1/D2/D3 properties (only those inherited from ABC):

    // MyGenericView.qml
    @Rectangle
    {
    property CustomClass myCustomObject
    property alias customProperty: myCustomObject.property // <<== ERROR

    onCustomPropertyChanged: // <<== ERROR too
    {
    console.log("YOUHOU")
    }
    }
    @

    Any idea what I should do?

    Thanks.



  • Hi, you should create resource instances and not properties, that is at least what I would do for c++ objects in QML. So instead of "property CustomClass myCustomObject" try a standalone object
    @
    CustomClass {
    id: myCustomObject
    onCustomPropertyChanged: ...
    }
    @
    that should work better and you can access it via the id, if you want to use it as a property I don't know if you can access the internal properties of another property (that is what you are trying to do).

    Btw this thread should go in the Qt Quick sub forum I guess :)



  • Thanks for your answer. But my container needs somehow to be generic, like running the "run" function of its attached CustomObject. With this solution, I will have to maintain one QML Wrapper for each of my custom object right?



  • I don't know what you mean, in your example you had a separate object for every Rectangle (it was just initialized with a property rather than a QML resource)?

    In QML you can also make use of inheritance like in c++, just put your Rectangle and the "CustomClass" object inside in a separate QML file and use that, then you only have to create objects ob that new file rather than doing the same thing if you need that multiple times.

    I hope you know what I mean, if not you can look at some QML examples maybe.



  • Thanks for your answer. I am not sure to understand what you mean actually.

    Suppose I have 100 CustomObject classes (like D1, D2, D3, etc) all inheriting from CustomObjectBaseClass. I don't want to do 100 QML item wrapping them. Ideally, I would like 1 QML item having a property to which I can attach any kind of CustomObjectD*. This QML item would be responsible to call the run function of the attached CustomObject.

    So based on you solution, I could maybe do something like:

    @// genericContainer.qml
    import myCustomObjectsLib 1.0
    Rectangle
    {
    <generic visual stuff here>
    }
    @

    @// myCustomObjectD1.qml
    import myCustomObjectsLib 1.0

    GenericContainer
    {
    CustomObjectD1{id: tool}

    function run()
    {
    tool.Run()
    }
    onTriggered: run()
    }
    @

    But, my main concern is that in this case, I will have to duplicate all "tool-related" code in each individual myCustomObjectD*.qml files which will get quickly impractical. So if possible, I would like to refactor all this by doing something like:

    @// genericContainer.qml
    import myCustomObjectsLib 1.0
    Rectangle
    {
    <generic visual stuff here>

    property CustomObjectBaseClass tool;
    

    function run()
    {
    tool.Run()
    }
    onTriggered: run()
    }
    @

    or something similar. Just note that I just begun with Qt / QML so I am still experimenting here and there;) Pardon me if I am slow to understand;)

    Best regards.



  • Well I don't know but your example should work, what is the problem with that?
    In your first post you wanted something different and that is not possible with a property in QML. so maybe you need to provide a better example what you really want to do or where your "tools" are coming from etc. are they all defined in your "myCustomObjectsLib" (c++ classes I assume)?

    But in general you can either use a property or a standalone object, but if you don't know then class to instantiate, you can only use a property similar to your example and inject the correct class later.


Log in to reply