QML: Make property readonly in derived class
qml, qt
Solution
Why not use inherited getter/setters for a private foo? something like :
//Base.qml
Item
{
QtObject
{
id : privateFoo
property int foo : 42
}
function getFoo() {return privateFoo.foo;}
function setFoo(newFoo) { privateFoo.foo = newFoo; }
}
//derived.qml
Base
{
function getFoo() {return 99;}
function setFoo(newFoo) { /*do nothing */ }
}
(I haven't tested this code)
Problem
I'm pretty sure the answer is no, but I thought I should ask anyway. Can an inherited property be made readonly/constant in a pure QML derived class? ``` // Base.qml Item { property int foo: 42 } // Derived.qml Base { foo: 99 // Make constant somehow! } ``` I'm currently tackling this by detecting a change in `foo` and printing an error to the console, but that's hardly a mark of good API... I have a menu item class which can represent menu entries, submenus, separators, etc. I need to specialise it's behaviour, but only for when it is in submenu mode. I can change the inheritance tree so I have a subclass for each menu type, but this seems silly as each subclass will only contain a single `readonly` property. However if there isn't another way, I will have to.