2017-05-28 60 views
0

我与QML写设置SettingsQML设置组合框

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

保存作品,但是当PROGRAMM开始设置得到由模型中的第一项自动覆盖:

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

我怎么能初始化与模型组合框并将其设置为存储的设置?

回答

0

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

+0

我尝试这样做:'属性字符串的SerialPort:serialName。 currentText“,但它也在启动时被覆盖: –

0

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

而不是在设置中存储currentText,存储Combobox的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");