gpt4 book ai didi

c++ - 如何为 QML 创建通用 ListModel

转载 作者:搜寻专家 更新时间:2023-10-31 01:35:38 25 4
gpt4 key购买 nike

我的应用程序包含许多列表,我使用 QAbstractListModel 派生的模型类在 QML-ListViews 中显示这些列表。它总是相同的,只是 Item-Type 不同。这就是为什么我想知道如何为这种方法构建类模板。

最佳答案

我发现,不可能在类模板中使用 Q_OBJECT-Macro。这就是为什么我的 GenericListModel 由两部分组成。

<强>1。通用列表模型数据

第一部分是模型本身,它派生自 QAbstractListModel 并实现了基本函数 data()、rowCount() 和 roleNames()。

<强>2。通用列表模型

第二部分是类模板,用作包装器以提供类似于 QListView 的功能。

如果您有任何建议或问题,请告诉我。如果能改进这个解决方案,那就太好了。

我在这里上传了完整的源代码: https://github.com/sebabebibobu/QGenericListModel/

<强>1。通用列表模型数据

QVariant GenericListModelData::data(const QModelIndex &index, int role) const
{
QObject *item = m_itemList.at(index.row());
return item->property(item->metaObject()->property(role).name());
}

/*
* Returns the number of items attached to the list.
*/
int GenericListModelData::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_itemList.size();
}

/*
* Generates a hash out of QMetaObject property-index and property-name.
*/
QHash<int, QByteArray> GenericListModelData::roleNames() const
{
QHash<int, QByteArray> roles;

if (!m_itemList.isEmpty()) {
for(int i = 0; i < m_itemList.at(0)->metaObject()->propertyCount(); i++) {
roles[i] = m_itemList.at(0)->metaObject()->property(i).name();
}
}

return roles;
}

/*
* Append Item to List.
*/
void GenericListModelData::appendItem(QObject *item)
{
/* map the notify()-signal-index with the property-index when the first item get's inserted */
if (m_itemList.isEmpty()) {
for(int i = 0; i < item->metaObject()->propertyCount(); i++) {
m_propertySignalIndexHash.insert(item->metaObject()->property(i).notifySignalIndex(), i);
}
}

/* connect each notify()-signals to the onDataChanged()-slot which call's the dataChanged()-signal */
for(int i = 0; i < item->metaObject()->propertyCount(); i++) {
connect(item, "2" + item->metaObject()->property(i).notifySignal().methodSignature(), this, SLOT(onDataChanged()));
}

/* finally append the item the list */
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_itemList.append(item);
endInsertRows();
}

/*
* Helper-Slot that emit's the dataChanged()-signal of QAbstractListModel.
*/
void GenericListModelData::onDataChanged()
{
QModelIndex index = createIndex(m_itemList.indexOf(sender()),0);

QVector<int> roles;
roles.append(m_propertySignalIndexHash.value(senderSignalIndex()));
emit dataChanged(index, index, roles);
}

<强>2。通用列表模型

template <typename T>
class GenericListModel : public GenericListModelData
{
public:
explicit GenericListModel(QObject *parent) : GenericListModelData(parent) {

}

void append(T *item) {
appendItem(item);
}

T *at(int i) {
return qobject_cast<T *>(m_itemList.at(i));
}
};

更新 01.05.2016

GrecKo 在评论中表示,像我这样的项目已经存在。这就是为什么我决定在这里也分享这个项目的链接:

http://gitlab.unique-conception.org/qt-qml-tricks/qt-qml-models

关于c++ - 如何为 QML 创建通用 ListModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36948212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com