如何从QML访问ListView的当前项目 [英] How to access ListView's current item from qml

查看:1102
本文介绍了如何从QML访问ListView的当前项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以存储和编辑笔记的应用程序.注释列表显示在这样的列表视图中:

I have an application that stores and edits notes. The list of notes is displayed in a listview like this:

Page {
        id: noteList
        title: i18n.tr("QNote")
        visible: false

        Column {
            anchors.fill: parent

            ListView {
                anchors.fill: parent
                model: notes
                delegate: ListItem.Standard {
                    text: Title
                    onClicked: editNote(NoteText, Title, modelData);
                    progression: true
                }
            }
        }
    }

function editNote(text, title, item) {
    pageStack.push(noteEdit, {title: title, text: text});
    handler.setActiveItem(item);
}

notes项目是一个NoteListModel,它是QAbstractListModel的子类并包含NoteListItems.我想做的是存储当前选定的NoteListItem,以便当用户想要保存修改后的笔记时,我可以轻松地访问其中的Note对象.但是,我不知道如何从qml委托访问支持的NoteListItem. modelData似乎是另外一回事.有什么办法吗?如果我可以将Note对象包装在QVariant中,则可以通过角色轻松访问它,但是当我尝试这样做时

The notes item is a NoteListModel that subclasses the QAbstractListModel and contains NoteListItems. What I would like to do is to store the currently selected NoteListItem so I could easily access the Note object inside when the user wants to save the modified note. However, I don't know how to access the backing NoteListItem from the qml delegate. the modelData seems to be something else. Is there any way to do so? If i could wrap the Note object in a QVariant I could access it easily through roles but when I tried it like this

QVariant NoteListItem::data(int role) {
    switch (role) {
    case Title:
        return note.getTitle();
    case NoteText:
        return note.getText();
    case NoteObject:
        return QVariant::fromValue(note);
    default:
        return QVariant();
    }
}

它导致编译器错误说

qmetatype.h:642:错误:将'sizeof'无效地应用于不完整的类型'QStaticAssertFailure'

qmetatype.h:642: error: invalid application of 'sizeof' to incomplete type 'QStaticAssertFailure'

还是我应该尝试从后备代码访问选定的列表项?有什么办法吗?道你有什么想法吗?

Or should i try to access the selected list item from the backing code? Is there any way for that? Dou you have any ideas?

感谢您的宝贵时间.问候, 彼得

Thanks for your time. Regards, Peter

推荐答案

这花了我很长时间才能找到,因为Stackoverflow上有许多不正确的解决方案.

This took me a very long time to find, as there are many incorrect solutions on Stackoverflow.

纯QML方式是使用DelegateModel并从QML访问它,如下所示:

The pure QML way is to use a DelegateModel and access it from QML as follows:

import QtQuick 2.4
import QtQml.Models 2.1

ListView {
    property var currentSelectedItem

    onCurrentItemChanged{
            // Update the currently-selected item
            currentSelectedItem = myDelegateModel.items.get(currentIndex).model;
            // Log the Display Role
            console.log(currentSelectedItem.display);
    }

    model: DelegateModel {
        id: myDelegateModel
        model: myAbstractItemModel
        delegate: {
            // Define delegates here
        }
    }
}

此行返回一个对象(var),您可以使用与委托中相同的方式对其进行访问: myDelegateModel.items.get(currentIndex).model

This line returns an object (var) that you can access in the same way as within a delegate: myDelegateModel.items.get(currentIndex).model

此示例假定您仅使用默认的DelegateModelGroup.

This example assumes you are only using the default DelegateModelGroup.

请参见 http://doc.qt.io/qt-5/qml-qtqml-models-delegatemodel.html http://doc.qt.io/qt-5/qml-qtqml-models-delegatemodelgroup.html#get-method 方法

这篇关于如何从QML访问ListView的当前项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆