QML中ListView的C++模型(三)

Model支持插入和删除行,需要自己添加插入和删除的函数,并且在插入函数中(插入数据前)调用

void QAbstractItemModel::beginInsertRows(const QModelIndex &parent, int first, int last)

插入数据完成后,调用

void QAbstractItemModel::endInsertRows()

类似地,在删除开始前调用:

void QAbstractItemModel::beginRemoveRows(const QModelIndex &parent, int first, int last)

删除完成后调用:

endRemoveRows();

在stringlistmodel.h文件中:

#ifndef STRINGLISTMODEL_H
#define STRINGLISTMODEL_H
#include <QAbstractListModel>
#include <QStringList>

class MyStringListModel : public QAbstractListModel
{
   
    Q_OBJECT
public:

    enum {
   
        DescriptionRole = Qt::UserRole,
    };

    explicit MyStringListModel(QObject *parent = nullptr);

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;

    QVariant data(const QModelIndex &index, int role) const override;

    virtual QHash<int, QByteArray> roleNames() const override;

    Qt::ItemFlags flags(const QModelIndex &index) const override;

    bool setData(const QModelIndex &index, const QVariant &value, int role) override;

    bool insertRows(int position, int rows, const QModelIndex &parent = QModelIndex()) override;

    bool removeRows(int position, int rows, const QModelIndex &parent = QModelIndex()) overr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值