使用 qml 在黑莓级联中的自定义选取器中设置默认值



我创建了一个显示国家/地区列表的选择器,并且工作正常。目前默认选择第一个国家/地区。我需要将其更改为另一个国家/地区。我应该怎么做?

这是我创建的选取器

 Picker {
            id: picker
            title: "Select your country"
            expanded:true
            verticalAlignment: VerticalAlignment.Fill
            // A DataModel is used to populate the picker.
            dataModel: XmlDataModel {
                source: "Model/CountryCodes.xml"
            }
            // Picker items are set up similarly as to how its done in a ListView.
            pickerItemComponents: [
                PickerItemComponent {
                    type: "country"
                    content: Container {
                        background: Color.create("#9B59B6")
                        layout: DockLayout {
                        }
                        Label {
                            multiline: true
                            maxWidth:  1000
                            text: pickerItemData.name
                            verticalAlignment: VerticalAlignment.Center
                            horizontalAlignment: HorizontalAlignment.Center
                            textStyle {
                                base:SystemDefaults.TextStyles.BodyText
                                color: Color.White
                                fontSize: FontSize.PointValue
                                fontSizeValue: 10.0
                                fontWeight: FontWeight.Normal
                            }
                        }
                    }
                }
            ]
                       onSelectedValueChanged: {
                // These are the currently selected indexes.
                var index0 = picker.selectedIndex(0);
                var type = dataModel.data([0, picker.selectedIndex(0)]);
                console.log("Selected index:"+index0);
                countryCode.mainText="+"+type.phoneCode;
            }    
        } 

我的国家代码.xml采用这种格式

  <model>
  <column loop="false" colspan="3" >
  <country code='af' phoneCode='93' name='Afghanistan' />
  <country code='al' phoneCode='355' name='Albania' />
  ........all other countries.....
  </column> 
  </model>

目前,阿富汗节目一直处于选中状态。

我用了这个,它从设置中加载哪一个。这被放置在onCreationCompleted

picker.select(0, app.getValueFor("backImageID", 0));

第二个元素是这样的

picker.select(1, app.getValueFor("fontColorID", 0));

最新更新