完整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()));
}