[Development] How to have equivalent of function over-riding in Qml

Alan Alpert 416365416c at gmail.com
Thu Jul 10 08:15:05 CEST 2014


On Wed, Jul 9, 2014 at 8:41 PM, travik <ravikiran.tallapalli at wipro.com> wrote:
>>
>> Thanks for the very quick reply.
>> Thank you for clarifying my understanding of correct derivation in Qml
>>
>> Regards,
>> R.kiran
>>
>
> Hi In continuation of the previous post - I now got it working to invoke
> the base class / derived class implementations correctly from extenal
> place.
> Now the question is: Can I have my derived class, call the base class
> implementation as part of its functionality?
> C++ equivalent would be something like this:
> class Base
> {
> public:
>   void method1()
>   {
>     std::cout << "Base::method1().." << std::endl;
>   }
> };
>
> class Derived: public Base
> {
> public:
>   void method1()
>   {
>     std::cout << "Derived::method1()..pre.." << std::endl;
>
>     // Derived class uses the base class implementation fully
>     // and does something extra on its own.. true case of extending the
>     // functionality, if I may say so!
>     Base::method1();
>
>     std::cout << "Derived::method1()..post.." << std::endl;
>   }
> };
>
> Qml Equivalent would be (to the extent I have figured out)
>
> Base.qml
> ----
> Item
> {
>    function method1()
>    {
>       console.log("Base::method1()...")
>    }
> }
>
> Derived.qml (in the same folder as Base.qml)
> ---
> Base
> {
>    function method1()
>    {
>       console.log("Derived::method1()...")
>    }
> }
>
> I tried this, it ended up in recursive loop, obviously!!
> Derived.qml
> ---
> Base
> {
>    id: myBase
>
>    function method1()
>    {
>       console.log("Derived::method1()...pre")
>
> //      myBase.method1() // <<--- calls the same method recursively
>
> //      Base.method1()  // <<--- this line gives undefined object,
> naturally!
>
>       console.log("Derived::method1()...post")
>    }
> }
>

This is not currently possible. I think a mechanism for it was
proposed at QtCS (can't find the notes) but is not yet implemented.

As a workaround, you can do something like the following
Base.qml
--
QtObject {
    property var method1: __method1
    function __method1() {}
}

Derived.qml
--
QtObject {
    method1: __method2
    function __method2() {__method1();}
}

Then, in other files where you call method1(), it uses the JS var
(pointing to the function) which had its valued changed in the derived
class.

--
Alan Alpert



More information about the Development mailing list