我的主页上有一个listview,它有一个自定义的listitem组件。我在Qsettings变量中有一个值,我可以在主页中访问该变量,但我无法在自定义listitem组件中访问该变量。加载主页时,Qsetting数据日志打印在主页的“创建完成”上,但在customlistitemhomepage的onCreationCompleted中显示错误。下面是我的主页结构和customlistitemhomepage结构的示例代码。
Page {
Container {
layout: DockLayout {
}
ScrollView {
id: homePageScroll
Container {
layout: DockLayout {
}
ListView {
id: contactListView
dataModel: contactsData
preferredHeight: homePageScroll.listHeight
overlapTouchPolicy: OverlapTouchPolicy.Allow
listItemComponents: [
ListItemComponent {
id: homeListComponent
type: "item"
CustomListItemHomePage {
id: listCell
}
//Some code
}
]
}
}
}
onCreationCompleted: {
console.log("Clipboard value:"+_settings.getValueFor("clipBoard", "No Data")); //THIS PRINTS CORRECTLY
}
}
}
我也在CustomListItemHomePage.qml
中记录了相同的内容 CustomListItemHomePage {
onCreationCompleted: {
console.log("Clipboard value:"+_settings.getValueFor("clipBoard", "No Data")); //THIS SHOWS ERROR CAN'T Find variable _settings
}
}
答案 0 :(得分:0)
listItemComponents无法从Listview外部调用函数。因此,如果您想在CustomListItemHomePage.qml中调用QSettings函数,您可以这样做:
ListView {
id: contactListView
dataModel: contactsData
preferredHeight: homePageScroll.listHeight
overlapTouchPolicy: OverlapTouchPolicy.Allow
listItemComponents: [
ListItemComponent {
id: homeListComponent
type: "item"
CustomListItemHomePage {
id: listCell
// Now you are able to call getClipboardValue() from ListItemComponent like
// listCell.ListItem.view.getClipboardValue()
}
//Some code
}
]
function getClipboardValue() {
return _settings.getValueFor("clipBoard", "No Data"));
}
}