2

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?

Jesse Jashinsky
  • 10,313
  • 6
  • 38
  • 63

3 Answers3

3

It is, instead of binding, use an assignment:

Component.onCompleted: text = SharedStrings.buttonOKString

Assignments will also break any existing bindings.

dtech
  • 47,916
  • 17
  • 112
  • 190
2

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.
Thomas McGuire
  • 5,308
  • 26
  • 45
1

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

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313