简体   繁体   中英

Qt Qml tree model menu with translations

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM