【问题标题】:How to insert/remove/edit rows in QAbstractListModel?如何在 QAbstractListModel 中插入/删除/编辑行?
【发布时间】:2020-03-19 11:29:34
【问题描述】:

所以,我有一个 QAbstractList 模型 - ContactBookModel。模型元素由Contact 类表示,该类包含构造函数以及namenumber 字段。

它有效,但只能查看,我无法编辑它。

然后我添加了这个方法:

Q_INVOKABLE bool add(const QString& name, const QString& number);

如果您需要实施:

bool ContactBookModel::add(const QString& name, const QString& number)
{
    try
    {
        if(name.isEmpty() || number.isEmpty())
        {
            return false;
        }
        beginInsertRows(QModelIndex(), rowCount(), rowCount());
        contacts.append(Contact(name, number));
        endInsertRows();
        return true;
    }
    catch(std::exception& e)
    {
        return false;
    }
}

这行得通,我从 QML 调用此方法,并从 TextFields 传递 2 个字符串作为参数。

但是文档说我需要重新实现insertRow() 方法。

好的,应该是这样的:

bool ContactBookModel::insertRow(int row, const QModelIndex &parent = QModelIndex())
{
   beginInsertRows(parent, row, row+1);
   contacts.append(/*what should be here?*/); //contacts is a private field in a ContactBookModel. They have got a QList<Contact> type. 
   endInsertRows();
}

1) 如您所见,当我使用 add 时,我在方法中构造了 Contact,使用两个字段。 insertRow() 怎么办?在哪里获得(构造)Contact

2) 如何删除联系人?文档说我应该重新实现removeRow()。如何在 QML 中使用它?

3) 如何从 QML 编辑联系人?

完整的项目代码在这里: https://github.com/bogdasar1985/ContactBook

【问题讨论】:

    标签: c++ qt model qml


    【解决方案1】:

    您想要一种方法来编辑模型中的元素。 所以你必须启用删除门的可点击性。您当前的代码不提供此功能,因此请添加 mousearea 并在单击元素时发出 signal

    接下来,您可以获取 currentIndex 名称和数字值,并在您的 addWindow 或您想要的任何窗口中使用它。

    import QtQuick 2.0
    
    Item {
        id: contact
        width: parent.width
        height: 30
    
        signal clicked()
    
    Rectangle {
        id: mainRect
        anchors.fill: parent
        border.color: "black"
        visible: true
        Text {
            id: nameText
            text: name
            anchors.left: parent.left
            anchors.leftMargin: 4
        }
        Text {
            id: numberText
            text: number
            anchors.right: parent.right
            anchors.rightMargin: 4
        }
    
        MouseArea {
            id: rectMousearea
            anchors.fill: mainRect
            hoverEnabled: true
            acceptedButtons: Qt.LeftButton
            cursorShape: Qt.PointingHandCursor
            onClicked: contact.clicked()
        }
    }
    }
    

    然后在Listview委托中

                delegate: Contact{
                    onClicked: {
                        addwindow.name = model.name
                        addwindow.number = model.number
                        addwindow.show()
                    }
                }
    

    addWindow.qml开头添加两个别名

    Window {
        id: addwindow
        visible: false
        width: 320
        height: 320
        title: qsTr("Add contact")
    
        property alias name: nameField.text
        property alias number: numberField.text
    

    namenumber 值相同时,请记住在add 方法中返回。或者你也可以从 qml 处理。

    希望对您有所帮助。

    【讨论】:

    • 这段代码应该做什么?目前,这段代码实现了我已经拥有的功能。请说明,这段代码的作用是什么?
    • 它有效,谢谢!但是当我开始编辑联系人时,什么都不做,然后按“确认”,最后插入相同的联系人。但是,更重要的是理解 C++ 方法。以及如何删除行?
    • 要使更改出现,您应该在namenumber 的Q_PROPERTY 中使用notify signal。因此,当您在委托中设置已编辑的值时,它们将触发更改信号,您将自动获得新值。
    • 但要编辑联系人,我们需要更改QList&lt;Contact&gt; contacts 的内容。会发生吗?
    猜你喜欢
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多