ActionSelectionPopover - 动态填充

ActionSelectionPopover - 动态填充

我正在尝试动态填充 ActionSelectionPopover...有谁能让它工作吗?

这是让它工作的静态方法:

ActionSelectionPopover {
    id: popMeUp
    data: bitrates
    delegate: ListItems.Standard {
        text: text
    }

    actions:  ActionList {
        id: myActions
        Action {
            text: "option 1"
            onTriggered: {
                popMeUp.hide()
                print(text)
            }
        }
    }
}

谢谢关注。

答案1

你可以使用强大的方法Qt.createQmlObject()使用 JS 函数的结果动态填充动作列表,如下所示:

import QtQuick 2.0
import Ubuntu.Components 0.1
import Ubuntu.Components.Popups 0.1

Rectangle {
    id: mainView
    width: units.gu(30) 
    height: units.gu(40)

    ActionSelectionPopover {
        id: actionSelectionPopover
    }


    Button {
        id: actionSelectionPopoverButton
        text: i18n.tr("action list")
        anchors.bottom: parent.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        width: units.gu(16)

        function my_actions(){
            var actions = []
            for (var i = 1; i <= 5; i++){
                var text = ['import Ubuntu.Components 0.1; Action {text: "', i.toString(), '";}'].join('')
                actions.push(Qt.createQmlObject(text, actionSelectionPopoverButton, "action"+i));
            }
            return actions;
        }

        onClicked: {
            actionSelectionPopover.actions = my_actions()
            actionSelectionPopover.caller = actionSelectionPopoverButton;
            actionSelectionPopover.show();
        }
    }
}

相关内容