qtjambi icon indicating copy to clipboard operation
qtjambi copied to clipboard

Add all java object constants as readonly properties when used in qml

Open mchistovib opened this issue 3 years ago • 0 comments

Is your feature request related to a problem? Please describe. Could you please add all class constant fields as readonly properties in qml by default. It's woud also work if you would only do it for classes that have some special annotation on them, but best solution would to avoid that - so we could also use classes of constants that come from 3rd party libraries and we can't modify them.

Describe the solution you'd like Lets say you have this class:

class SomeClass extends QObject {
    public final int intConst = 42;
    publis final String stringConst = "test";
    public final SomeOtherClass objectConst = new SomeOtherClass()
}

When we move that object to qml, for example using qmlEngine.rootContext().setContextProperty("constTest", new SomeClass);, we should be able to access it's properties like this: console.error("ConstTest intConst = " + constTest.intConst + " other const =" + constTest.objectConst.someMethod() ); It is safe to do so because these constant fields will never change so you can safely create reader and const flag on the jambi side

Describe alternatives you've considered You could also add a reader method with @QtPropertyReader() annotation for every field, but that would lead to a lot of boilerplate code without any usefulness, also it would still throw "notify signal missing error" although the field is readonly. It is commom to have pure constant field classes in java with hundreds of fields, so making a separate reader for each of them takes a lot of time and gives zero benefit - you can't change them anyway, so no point to cover them with reader function usage. In fact, in Java the main reason to use constants is to avoid using read methods and just use direct access since you know the field can't change

mchistovib avatar Oct 07 '22 16:10 mchistovib