[Qt] QML ListView与C++交互(Using C++ Models with Qt Quick Views)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[Qt] QML ListView与C++交互(Using C++ Models with Qt Quick Views),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5390字,纯文字阅读大概需要8分钟。
内容图文
![[Qt] QML ListView与C++交互(Using C++ Models with Qt Quick Views)](/upload/InfoBanner/zyjiaocheng/845/b0a9a5641c0340cd937a3b1c79340927.jpg)
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()));
}
内容总结
以上是互联网集市为您收集整理的[Qt] QML ListView与C++交互(Using C++ Models with Qt Quick Views)全部内容,希望文章能够帮你解决[Qt] QML ListView与C++交互(Using C++ Models with Qt Quick Views)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。