如何为QML创建通用ListModel

How to create a Generic ListModel for QML

本文关键字:ListModel 创建 QML      更新时间:2023-10-16

我的应用程序由许多列表组成,我使用QAbstractListModel派生的模型类在QML ListViews中显示这些列表。它总是一样的,只是项目类型不同。这就是为什么我想知道如何为这种方法构建一个类模板。

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

1.GenericListModelData

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

2.GenericListModel

第二部分是类模板,它被用作包装器,以提供类似于QListView的函数。

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

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

1.GenericListModelData

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.GenericListModel

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