【问题标题】:QML settings for combobox组合框的 QML 设置
【发布时间】:2017-05-28 09:48:50
【问题描述】:

我正在使用 qml Settings 编写设置

Settings {
    id: powerTuneSettings
    property alias serialPortName: serialName.currentText
}

保存工作,但是当程序启动时,设置会被模型中的第一个条目覆盖:

ComboBox {
    id: serialName
    width: 200
    model: Serial.portsNames              
}

如何使用模型初始化组合框并将其设置为存储的设置?

【问题讨论】:

    标签: qt qml


    【解决方案1】:

    可能的解决方案是将其存储为property string,然后在ComboBoxComponent.onCompleted 回调中在模型中搜索该字符串。如果找到,请设置currentIndex

    【讨论】:

    • 我试过这个:property string SerialPort: serialName.currentText 但它也在启动时被覆盖:
    【解决方案2】:

    ComboboxcurrentText 属性是只读的,这意味着您不能直接设置它。要选择 Combobox 的当前项,您必须设置其 currentIndex

    不要在“设置”中存储currentText,而是存储组合框的currentIndex,它应该可以按预期工作。

    ComboBox {
        id: serialName
        width: 200
        model: Serial.portsNames
    }
    
    Settings
    {
        property alias currentIndex: serialName.currentIndex
    }
    

    请注意,为了使 QML 设置正常工作,您可能必须在 main.cpp 中设置应用的组织名称或组织域

    app.setOrganizationName("yourOrg");
    app.setOrganizationDomain("domain.org");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 1970-01-01
      相关资源
      最近更新 更多