【问题标题】:QML ComboBox item does not display correct textQML ComboBox 项目不显示正确的文本
【发布时间】:2018-11-23 16:31:43
【问题描述】:

组合框的某些字符似乎有问题,至少我检测到当我们转到项目列表时,“&”显示为“_”。 我们如何解决这个问题?

一个例子:

ComboBox {
    id: combobox
    textRole: "text"
    Layout.fillWidth: true

    model: ListModel {
        dynamicRoles: true
    }

    Component.onCompleted: {
        reload()
    }

    Connections {
        target: trans // this is a translator from a git project you are referring to
        onLanguageChanged: {
            combobox.reload()
        }
    }

    function reload() {
        var i = combobox.currentIndex
        combobox.model = [
                    {text: qsTr("apple & orange")}
                ]
        combobox.currentIndex = i
    }

这是在 QT 5.11.2 上

如果我转义文本 {text: qsTr("apple && orange")}

发生这种情况

【问题讨论】:

    标签: qt qml


    【解决方案1】:

    Qt 使用& 符号为特定的 UI 元素指定助记键,因此使用该符号。

    只需改用&&。

    至于显示文本错误,有一个简单的方法可以解决:

      ComboBox {
        model: ["a and b", " a && b"]
        displayText: currentText.replace("&&", "&")
      }
    

    【讨论】:

    • 对列表中的项目有效,但组合框中的选定项目被窃听。如何修复?看我的图片。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多