0

我尝试为 ListView 上的多个项目选择处理一组操作。上下文:RSS 阅读器。一篇文章可以是已读/未读、已标记/未标记、已发布/未发布。目前,当用户启动多选时,我的代码添加了所有可能的操作:标记为已读、保持未读……因此,即使每个选定的文章都已标记为已读,标记为已读也是可用的。

我试图隐藏不相关的行为。文档中有一个示例可以在多个选择处理程序之间切换,并带有预定义的操作列表。我需要为每种可能性创建一个处理程序,因此需要 8 个处理程序。这显然不是解决方案。

MultiSelectionHandler::removeAllActions()每次我的选择发生变化时,我都会尝试调用并MultiSelectionHandler::addAction()添加回所需的操作。这是我当前代码的相关部分:

ListView {
    dataModel: _articleModel
    id: listView

    multiSelectAction: MultiSelectActionItem {
    }

    multiSelectHandler {
        status: qsTr("None selected")
        actions: []

        attachedObjects: [
            ActionItem {
                id: actionMultiRead
                title: qsTr("Mark as read")
                imageSource: "asset:///images/mark_as_read.png"

                onTriggered: {
                    var selectionList = listView.selectionList();
                    listView.clearSelection();
                    for (var i = 0; i < selectionList.length; ++i)
                        _articleModel.data(selectionList[i]).unread = false;
                }
            },
            ActionItem {
                id: actionMultiUnread
                title: qsTr("Keep unread")
                imageSource: "asset:///images/keep_unread.png"

                onTriggered: {
                    var selectionList = listView.selectionList();
                    listView.clearSelection();
                    for (var i = 0; i < selectionList.length; ++ i)
                        _articleModel.data(selectionList[i]).unread = true;
                }
            },

            ActionItem {
                id: actionMultiPublish
                title: qsTr("Publish")
                imageSource: "asset:///images/publish.png"

                onTriggered: {
                    var selectionList = listView.selectionList();
                    listView.clearSelection();
                    for (var i = 0; i < selectionList.length; ++ i)
                        _articleModel.data(selectionList[i]).published = true;
                }
            },
            ActionItem {
                id: actionMultiUnpublish
                title: qsTr("Unpublish")
                imageSource: "asset:///images/unpublish.png"

                onTriggered: {
                    var selectionList = listView.selectionList();
                    listView.clearSelection();
                    for (var i = 0; i < selectionList.length; ++ i)
                        _articleModel.data(selectionList[i]).published = false;
                }
            },

            ActionItem {
                id: actionMultiStar
                title: qsTr("Star")
                imageSource: "asset:///images/star.png"

                onTriggered: {
                    var selectionList = listView.selectionList();
                    listView.clearSelection();
                    for (var i = 0; i < selectionList.length; ++ i)
                        _articleModel.data(selectionList[i]).marked = true;
                }
            },
            ActionItem {
                id: actionMultiUnstar
                title: qsTr("Unstar")
                imageSource: "asset:///images/unstar.png"

                onTriggered: {
                    var selectionList = listView.selectionList();
                    listView.clearSelection();
                    for (var i = 0; i < selectionList.length; ++ i)
                        _articleModel.data(selectionList[i]).marked = false;
                }
            }
        ]
    }

    onSelectionChanged: {
        if (selectionList().length > 1) {
            multiSelectHandler.status = qsTr("%1 items selected").arg(selectionList().length);
        } else if (selectionList().length == 1) {
            multiSelectHandler.status = qsTr("1 item selected");
        } else {
            multiSelectHandler.status = qsTr("None selected");
        }

        // Update available actions
        multiSelectHandler.removeAllActions();
        for (var i = 0; i < selectionList().length; ++ i) {
            var elt = _articleModel.data(selectionList()[i]);
            if (elt.marked) {
                multiSelectHandler.addAction(actionMultiUnstar);
                console.log("Adding unstar");
            } else {
                multiSelectHandler.addAction(actionMultiStar);
                console.log("Adding star");
            }

            if (elt.published) {
                multiSelectHandler.addAction(actionMultiUnpublish);
                console.log("Adding unpublish");
            } else {
                multiSelectHandler.addAction(actionMultiPublish);
                console.log("Adding publish");
            }

            if (elt.unread) {
                multiSelectHandler.addAction(actionMultiRead);
                console.log("Adding read");
            } else {
                multiSelectHandler.addAction(actionMultiUnread);
                console.log("Adding unread");
            }
        }
    }
}

虽然它可以大大优化并且在选择更改之前可以正常工作,但在更改后它不再起作用:MultiSelectionHandler::removeAllActions()删除操作,它们不能被添加回来。

有没有办法在不声明 8 个多选处理程序的情况下实现这一点?

4

1 回答 1

1

这个问题已经很老了,可能你已经解决了,但是对于任何其他偶然发现这个问题的人,我的解决方案是使用MultiSelectionHandler::removeAction()而不是 removeAllActions()。

removeAllActions 会自动删除对象,因此每次都应该重新创建动作。

for (var i = 0; i < multiSelectAction.actionCount(); ++ i)
     multiSelectAction.removeAction(multiSelectAction.actionAt(i));

干杯。

于 2014-05-23T10:35:27.330 回答