0

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.

  • Why don't you use a model instead of `QQmlListProperty`? Models initially support data changing handling etc. – folibis Aug 18 '20 at 09:42
  • The problem is not with changing the data. The problem is with translating whatever is in it to different languages on the fly. – Vegard H Aug 18 '20 at 10:22
  • Also i did try with just a MenuObject exposed as a q_property directly without the text changing upon retranslating with qmlengine. – Vegard H Aug 18 '20 at 10:29

1 Answers1

1

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.

user268396
  • 11,576
  • 2
  • 31
  • 26
  • You are saying that the MenuObject's Qproperty should be redone as: Q_PROPERTY(QString description READ getDescription NOTIFY descriptionChanged); But what triggers the notify? I tried hooking this up to the translators emitted signal "languageChanged", and it does do the notify, but alas the text is not translated. – Vegard H Aug 18 '20 at 08:46