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

查看:103
本文介绍了如何从 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.htmlhttp://doc.qt.io/qt-5/qml-qtqml-models-delegatemodelgroup.html#get-method 方法

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

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