Reputation: 10683
In my QT application, I want to move all of my UI strings to a central file, such as below:
SharedStrings.qml:
import QtQuick 2.0
pragma Singleton
QtObject {
readonly property string buttonOKString: "OK"
readonly property string buttonCancelString: "CANCEL"
}
Example.qml:
text: SharedStrings.buttonOKString
The only issue is that, as I understand it, this creates a property binding, which allow Example.qml's text to update whenever the value in SharedStrings.qml changes. However, this is unneeded since these values will not change. This is an embedded app so any memory/processing I can save is beneficial.
Is there a way I can define strings in a single file and used in other qml files, without using property binding?
Upvotes: 2
Views: 1438
Reputation: 98505
On the current implementation of QML's JavaScript engine, property bindings only incur overhead when things change.
Upvotes: 1
Reputation: 5466
I see three other ways to do that here:
Q_PROPERTY
s that are CONSTANT
. While that is probably the fastest, it is also the most code to write, as you need a getter function for each constant. Therefore I wouldn't recommend it.QQmlContext::setContextProperty("myVar", myVarValue);
for each variable. Downside is that those are then global variables, so again I would not recommend it. Upvotes: 2
Reputation: 49329
It is, instead of binding, use an assignment:
Component.onCompleted: text = SharedStrings.buttonOKString
Assignments will also break any existing bindings.
Upvotes: 3