Is it possible to display and use the content of a read-only key-value map at the auto complete suggestion?
-
Hello @ All!
I stuck with a problem which I cannot overcome by myself at this moment, so hopefully someone at this forum can point me to the correct direction or tell me what to do.
Any helpful input is appreciated.Short description:
I am searching for a way to get a key-value list or map from c++ across to qml AND still be able to use the auto-completion suggestions from QtCreator with the content of the keys at qml side.In detail:
At c++ side I want to read an external file with a collection of default values and parse it to a map like key-value structure. This key-value structure should be readable by qml through e.g. an interface class.
It is not known what the keys in the list are, so the "quicksearch" - by that I mean the auto completion list-like suggestion from QtCreator - should be able to display the content of the keys to choose from.In the end I want to do something like this at the qml side:
Text { id: xx text: cInterface.defaultValues.TemperatureMax }
Where
- defaultValues is the map/list
- TemperatureMax is the key
My problem:
I experimented with QQmlListProperty (QObject with 2 properties (name&value) as content ) class and QQmlPropertyMap class. With both classes I was able to display the content, BUT- With QQmlListProperty I could only access the value/object through an index. But it is not know what keys is available and what the index for that key is.
- With QQmlPropertyMap I was able to do what I wanted, but I was not able to set it to read-only and found nothing helpful during my google search.
When using QQmlPropertyMap as a Q_PROPERTY I got my read-only property, but was not able to see the content of the keys at the auto completion suggestion.
By the way, I am not stuck with QQmlListProperty or QQlmPropertyMap. It just looked like these two could be the most useful to my case.
-
@Abayo said in Is it possible to display and use the content of a read-only key-value map at the auto complete suggestion?:
It is not known what the keys in the list are, so the "quicksearch" - by that I mean the auto completion list-like suggestion from QtCreator
QtC won't suggest you map keys or values. It does not do that even in pure C++.
text: cInterface.defaultValues.TemperatureMax
Here is what should work (but is hackish... not a nice solution): create a QObject and pass it to QML (that will be your
defaultValues
object). When you read the values file (I imagine this happens at runtime, right?), add each key-value pair as a new property withdefaultValues.setProperty(key, value)
. Then QML should be able to pick it up.If you mean, however, that all this should work at design stage (not at runtime), then it's just not possible to do. The best solution I can recommend here is to use a separate QML file as a "container" for your default values, for example:
// Defaults.qml QtObject { property int someValue1: 5 property string someValue2: "hi" }
-
Thanks for your reply!
You are right, the parsing of the external file will happen during runtime, so I will try your suggestion.Another way which I thought of would be to fill an enumeration list with the keys in the correct order and use these enumerations to address the index of the QQmlListProperty.
So to say: cInterface.defaultValues["TemperatureMax"]But this requires that the enumeration and the list are in sync/the same order at every time, or else you will get the wrong value... so your suggestions sounds easier.