[Qt] QML ListView与C++交互(Using C++ Models with Qt Quick Views)

8 篇文章 0 订阅
2 篇文章 0 订阅

完整QML视频教程:http://i.xue.taobao.com/detail.htm?courseId=113958

1.数据模型类:

头文件:

#ifndef INPUTGROUP_H
#define INPUTGROUP_H

#include <QObject>
#include <QJsonObject>

class InputGroup
{
    Q_GADGET
    Q_PROPERTY(int id READ id WRITE setId)
    Q_PROPERTY(QString name READ name WRITE setName)

public:
    InputGroup();
    InputGroup(const QJsonObject &dict);

    inline int id() const { return m_id; }
    inline QString name() const { return m_name; }

    inline void setId(int id) { m_id = id; }
    inline void setName(const QString &name) { m_name = name; }

    void loadJsonObject(const QJsonObject &dict);

private:
    int m_id = 0;
    QString m_name;
};

typedef QList<InputGroup> InputGroupList;

Q_DECLARE_METATYPE(InputGroup)

#endif // INPUTGROUP_H

源文件:

#include "inputgroup.h"

InputGroup::InputGroup()
{

}

InputGroup::InputGroup(const QJsonObject &dict)
{
    loadJsonObject(dict);
}

void InputGroup::loadJsonObject(const QJsonObject &dict)
{
    m_id = dict["id"].toInt();
    m_name = dict["name"].toString();
}

2.QAbstractListModel交互类:

#ifndef INPUTGROUPMODEL_H
#define INPUTGROUPMODEL_H

#include <QAbstractListModel>
#include "inputgroup.h"

class InputGroupModel : public QAbstractListModel
{
    Q_OBJECT
    Q_PROPERTY(int count READ rowCount NOTIFY rowCountChanged)

public:
    enum InputGroupRoles {
        IdRole = Qt::UserRole + 1,
        NameRole
    };

    explicit InputGroupModel(QObject *parent = nullptr);

    // Basic functionality:
    inline QHash<int, QByteArray> roleNames() const override;
    inline int rowCount(const QModelIndex &parent = QModelIndex()) const override;

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

    // Editable:
    bool setData(const QModelIndex &index, const QVariant &value,
                 int role = Qt::EditRole) override;

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

    Q_INVOKABLE inline void append(const InputGroup &model);
    Q_INVOKABLE inline void append(const InputGroupList &list);
    Q_INVOKABLE inline void append(const QJsonObject &dict);
    Q_INVOKABLE void clear();
    Q_INVOKABLE InputGroup get(int index) const;
    Q_INVOKABLE void insert(int index, const InputGroup &model);
    Q_INVOKABLE void insert(int index, const QJsonObject &dict);
    Q_INVOKABLE void remove(int index, int count = 1);
    Q_INVOKABLE void set(int index, const InputGroup &model);
    Q_INVOKABLE void set(int index, const QJsonObject &dict);
    Q_INVOKABLE void setProperty(int index, const QString &property,
                                 const QVariant &value);

signals:
    void rowCountChanged(int count);

private:
    QHash<int, QByteArray> m_roles;
    InputGroupList m_contents;
};

QHash<int, QByteArray> InputGroupModel::roleNames() const
{
    return m_roles;
}

int InputGroupModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;

    return m_contents.count();
}

void InputGroupModel::append(const InputGroup &model)
{
    insert(m_contents.length(), model);
}

void InputGroupModel::append(const InputGroupList &list)
{
    beginResetModel();
    m_contents.append(list);
    endResetModel();
}

void InputGroupModel::append(const QJsonObject &dict)
{
    insert(m_contents.length(), dict);
}

#endif // INPUTGROUPMODEL_H

#include "inputgroupmodel.h"

InputGroupModel::InputGroupModel(QObject *parent)
    : QAbstractListModel(parent)
{
    m_roles.insert(IdRole, "id");
    m_roles.insert(NameRole, "name");
}

QVariant InputGroupModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    switch (role) {
    default:
        break;
    case IdRole:
        return m_contents[index.row()].id();
    case NameRole:
        return m_contents[index.row()].name();
    }

    return QVariant();
}

bool InputGroupModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid() || !value.isValid())
        return false;
    if (0 > index.row() || index.row() >= m_contents.length())
        return false;
    if (data(index, role) == value)
        return false;

    switch (role) {
    default:
        break;
    case IdRole:
        m_contents[index.row()].setId(value.toInt());
        break;
    case NameRole:
        m_contents[index.row()].setName(value.toString());
        break;
    }

    emit dataChanged(index, index, QVector<int>() << role);

    return true;
}

Qt::ItemFlags InputGroupModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::NoItemFlags;

    return Qt::ItemIsEditable;
}

void InputGroupModel::clear()
{
    beginResetModel();

    m_contents.clear();
    emit rowCountChanged(m_contents.count());

    endResetModel();
}

InputGroup InputGroupModel::get(int index) const
{
    if (0 <= index && index < m_contents.count())
        return m_contents.at(index);

    return InputGroup();
}

void InputGroupModel::insert(int index, const InputGroup &model)
{
    Q_ASSERT(0 <= index && index <= m_contents.count());

    beginInsertRows(QModelIndex(), index, index);

    m_contents.insert(index, model);
    emit rowCountChanged(m_contents.count());

    endInsertRows();
}

void InputGroupModel::insert(int index, const QJsonObject &dict)
{
    insert(index, InputGroup(dict));
}

void InputGroupModel::remove(int index, int count)
{
    if (index < 0 || index >= m_contents.count())
        return ;
    if (index + count - 1 >= m_contents.count())
        return ;

    beginRemoveRows(QModelIndex(), index, index + count - 1);

    while (--count >= 0) {
        m_contents.removeAt(index);
    }

    emit rowCountChanged(m_contents.count());

    endRemoveRows();
}

void InputGroupModel::set(int index, const InputGroup &model)
{
    setData(this->index(index), model.id(), IdRole);
    setData(this->index(index), model.name(), NameRole);
}

void InputGroupModel::set(int index, const QJsonObject &dict)
{
    set(index, InputGroup(dict));
}

void InputGroupModel::setProperty(int index, const QString &property,
                                  const QVariant &value)
{
    if (0 <= index && index < m_contents.count())
        setData(this->index(index), value, m_roles.key(property.toUtf8()));
}

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值