Qt Qml tree model menu with translations

306 Views Asked by At

I am trying to make an app with live translation of text in a large tree model menu structure, in the same manner as: https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/itemviews/simpletreemodel?h=5.15

The item's "data" is a QString that is translated like

root = new MenuObject(tr("Main menu"));

And children are appended like:

root->appendChild(new MenuObject(tr("Test 1")))
    .appendChild(new MenuObject(tr("Test 2")))

I am using QML to show these, with a qmllistpoprerty to show these menus like:

Q_PROPERTY(QQmlListProperty<MenuObject> list READ getList NOTIFY listChanged);

The QML is a simple ListView with a delegate Label showing the MenuObjects's description with the q_property:

Q_PROPERTY(QString description READ getDescription CONSTANT);

To change language i am using a function getting the translation file into the translator, followed by:

installTranslator(translator);
engine.retranslate();

Now this does work for simple q_properties like:

Q_PROPERTY(QString header READ getHeader NOTIFY listChanged);

Where

QString MainMenu::getHeader(){
    return tr("Header");
}

But I CANNOT get the translations to work for the items in the treemodel. Any help is appreciated.

1

There are 1 best solutions below

1
user268396 On

If your description prop never fires an updated signal, then your UI will never refresh it.

The reason it works for Q_PROPERTY(QString header READ getHeader NOTIFY listChanged); is because presumably the listChanged() signal is fired whenever header is supposed to change also.

To fix it, you need to declare an appropriate NOTIFY signal for your description, and of course it is no longer a CONSTANT.