简体   繁体   English

Qt Qml 树 model 带有翻译的菜单

[英]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 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该项目的“数据”是一个 QString 被翻译为

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:我正在使用 QML 来显示这些,并使用 qmllistpoprerty 来显示这些菜单,例如:

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: QML 是一个简单的 ListView,带有一个委托 Label,显示带有 q_property 的 MenuObjects 描述:

Q_PROPERTY(QString description READ getDescription CONSTANT);

To change language i am using a function getting the translation file into the translator, followed by:要更改语言,我使用 function 将翻译文件输入翻译器,然后:

installTranslator(translator);
engine.retranslate();

Now this does work for simple q_properties like:现在这确实适用于简单的 q_properties,例如:

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.如果您的description道具永远不会触发更新信号,那么您的 UI 将永远不会刷新它。

The reason it works for Q_PROPERTY(QString header READ getHeader NOTIFY listChanged);它适用于Q_PROPERTY(QString header READ getHeader NOTIFY listChanged); is because presumably the listChanged() signal is fired whenever header is supposed to change also.是因为大概只要header也应该改变,就会触发listChanged()信号。

To fix it, you need to declare an appropriate NOTIFY signal for your description , and of course it is no longer a CONSTANT .要修复它,您需要为您的description声明一个适当的NOTIFY信号,当然它不再是CONSTANT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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