例如,我的 QML 中有这样的模型:
ListModel{
id: mainlist
ListElement
{
name: "Name1"
type: "subMenu"
toogle: false
iconSource: ""
function actionClick()
{
console.log("actionclick is passed for 0 item!")
}
}
ListElement
{
name: "Manage Favorites"
type: "subMenu"
toogle: false
iconSource: "image://provider/common/endless_menu/list_icons/fav"
function actionClick()
{
console.log("actionclick is passed for 1 item!")
}
}
ListElement
{
name: "Name2"
type: "subMenu"
toogle: false
iconSource: "image://provider/common/endless_menu/list_icons/active"
function actionClick()
{
console.log("actionclick is passed for 2 item!")
}
}
ListElement
{
name: "Name3"
type: "subMenu"
toogle: false
iconSource: "image://provider/common/endless_menu/list_icons/scan"
function actionClick()
{
console.log("actionclick is passed for 3 item!")
}
}
ListElement
{
name: "Manual Frequency Input"
type: "commonBtn"
toogle: false
iconSource: ""
function actionClick()
{
console.log("actionclick is passed for 4 item!")
}
}
function onStart(currIndex)
{
console.log("test is passed for " + currIndex + "item!")
}
}
所以,我有 ListView 元素id: optionlist
和 ListDelegate 用于控制列表元素。我有几个模型 - 所有这些元素都是为项目中的菜单选项创建的。因此,onModelChanged: optionlist.model.onStart()
列表文件中的代码可以完美运行。
问题是从MouseArea 元素中actionClick()
的 listDelegate 脚本调用函数。OnClicked
是否有可能做到这一点?像这样的东西:optionlist.model.ContentItem.children[currentIndex].actionClick()
也许还是别的什么?
更新:对不起,Amit Tomer,也许我没有正确解释任务......所以,我需要模型中的元素和下一个字段:
name: - text for element
type: - type of element (button, radio button, check button or submenu) - needed for corect action when user clicked on this item
toogle: - boolean value for radio/check buttons state, and for some internal operations.
iconSource: - path for icon, if it needed.
function actionClick() - function, which will be execute when user clicked on this item.
所有这些都必须完成,以便清楚且轻松地填写所有选项菜单树。此菜单树将写入单独的文件中。
在下面的代码中,我展示了工作模型:
Item{
id: menuoptions
property ListModel prev: manageFavorites
property bool root: true
//Main Menu
property alias mainlist: mainlist
ListModel{
id: mainlist
ListElement
{
name: "Band: "
type: "subMenu"
toggle: false
iconSource: ""
}
ListElement
{
name: "Manage Favorites"
type: "subMenu"
toggle: false
iconSource: "image://provider/common/endless_menu/list_icons/fav"
}
ListElement
{
name: "Show: "
type: "subMenu"
toggle: false
iconSource: "image://provider/common/endless_menu/list_icons/active"
}
ListElement
{
name: "Scan"
type: "subMenu"
toggle: false
iconSource: "image://provider/common/endless_menu/list_icons/scan"
}
ListElement
{
name: "Manual Frequency Input"
type: "commonBtn"
toggle: false
iconSource: ""
}
function actionClick(currIndex)
{
switch(currIndex)
{
case 0:
{
prev = mainlist
menuList.model = bandlist
break
}
case 1:
{
prev = mainlist
menuList.model = manageFavorites
break
}
case 2:
{
prev = mainlist
menuList.model = showlist
break
}
case 3:
{
console.log("Scan started")
mainlist.setProperty(3, "name", getScan())
break
}
case 4:
{
console.log("Speller for Manual Frequency Input open!")
break
}
}
}
function onStart()
{
console.log("root model loaded")
root = true
mainlist.setProperty(0, "name", "Band: " + getBand())
mainlist.setProperty(2, "name", "Show: " + getShow())
mainlist.setProperty(3, "name", getScan())
}
}
//First Lvl subMenu
property alias bandlist: bandlist
ListModel{
id: bandlist
... Analog menulist
}
property alias manageFavorites: manageFavorites
ListModel{
id: manageFavorites
... Analog menulist
}
property alias showlist: showlist
ListModel{
id: showlist
... Analog menulist
}
}
如你所见,我写了类似你说的东西。没关系,如果我没有找到更好的解决方案。更好的解决方案 - 删除 actionClick() 通用函数并将其部分(案例块中的代码)分别添加到 ListElement。并在元素点击时调用它。
我不知道如何从 ListElement 调用这个函数。在 WPF 中,我将只创建自定义组件,它可以替代 ListElement 和所有组件!但我不知道如何在 QML 中做到这一点。
我有什么不清楚的 - 请询问。
更新:问题解决了。我的答案变体。谢谢大家。