Resolving property and function “overrides” in QML

前端 未结 1 596
不知归路
不知归路 2020-12-21 00:04

It seems like although QML supports \"overriding\" of properties and functions, the support is somewhat clunky. Here is an example snippet:

// T1.qml
QtObjec         


        
相关标签:
1条回答
  • 2020-12-21 00:15

    Found one simple and naive manual solution:

    // T1.qml
    QtObject {
        function f() { return f_t1() }
        function f_t1() { return 1 }
    }
    
    // T2.qml
    T1 {
        function f() { return f_t2() }
        function f_t2() { return f_t1() + " blah " }
    }
    
    // usage
    T2 {
        function f() { return f_t2() + 1.5}
        Component.onCompleted: console.log(f()) // outputs 1 blah 1.5
    }
    

    In short, have explicitly named function for every level of the "inheritance" that overrides, and use the non-decorated function override for polymorphic access, thus now the "base" type implementations can be reused by the derived.

    0 讨论(0)
提交回复
热议问题