Jesse Jashinsky
Jesse Jashinsky

Reputation: 10683

Is it possible to avoid property binding?

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

Answers (3)

On the current implementation of QML's JavaScript engine, property bindings only incur overhead when things change.

Upvotes: 1

Thomas McGuire
Thomas McGuire

Reputation: 5466

I see three other ways to do that here:

  1. Use .js files, as described in this answer - probably the easiest
  2. Write a C++ class with lots of Q_PROPERTYs 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.
  3. From C++, call QQmlContext::setContextProperty("myVar", myVarValue); for each variable. Downside is that those are then global variables, so again I would not recommend it.

Upvotes: 2

dtech
dtech

Reputation: 49329

It is, instead of binding, use an assignment:

Component.onCompleted: text = SharedStrings.buttonOKString

Assignments will also break any existing bindings.

Upvotes: 3

Related Questions