QObject 的集合,如 ListModel

Collection of QObjects like ListModel

我想创建一个包含 ShortcutItem 列表 作为 QObject,然后将 ShortcutItem 添加到其中。例如,我有这个:

#include <QObject>

class ShortcutItem : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
    Q_PROPERTY(QString action READ action WRITE setAction NOTIFY actionChanged)
    Q_PROPERTY(QString icon READ icon WRITE setIcon NOTIFY iconChanged)
    Q_PROPERTY(QString font READ font WRITE setFont NOTIFY fontChanged)
    Q_PROPERTY(int size READ size WRITE setSize NOTIFY sizeChanged )
public:
    explicit ShortcutItem(QObject *parent = nullptr);
    QString title() const;
    QString action() const;
    QString icon() const;
    QString font() const;
    int size() const;
    void setTitle(const QString &title);
    void setAction(const QString &action);
    void setIcon(const QString &icon);
    void setFont(const QString &font);
    void setSize(const int &size);
private:
    QString _title;
    QString _action;
    QString _icon;
    QString _font;
    int _size;
signals:
    void titleChanged(QString title);
    void actionChanged(QString action);
    void iconChanged(QString icon);
    void fontChanged(QString font);
    void sizeChanged(int size);
public slots:
};

我可以像这样在 QML 中使用它:

U.Shortcut{
     title: "title"
     icon: "\uf015"
     font: "fontawesome"
     action: "open"
     size: 1
}

但我想创建一个快捷方式列表(类似于其中包含 ListElements 的 ListModel),如下所示:

U.Shortcuts {
    U.Shortcut {

    }
    U.Shortcut {

    }
}

我该如何创建它?
https://doc.qt.io/qt-5/qtqml-cppintegration-definetypes.html#specifying-default-properties-for-qml-object-types 了解更多相关信息的好地方

您必须创建一个 QObject,其中 Q_PROPERTY 作为 QQmlListProperty<ShortcutItem>DefaultProperty 作为 Q_PROPERTY 本身:

shortcutcollection.h

#ifndef SHORTCUTCOLLECTION_H
#define SHORTCUTCOLLECTION_H

#include <QObject>
#include <QVector>
#include <QQmlListProperty>
#include "shortcutitem.h"

class ShortcutCollection: public QObject
{
    Q_OBJECT
    Q_PROPERTY(QQmlListProperty<ShortcutItem> items READ items)
    Q_CLASSINFO("DefaultProperty", "items")
public:
    ShortcutCollection(QObject *parent=nullptr);
    QQmlListProperty<ShortcutItem> items();
    int itemsCount() const;
    ShortcutItem *item(int) const;
private:
    QList<ShortcutItem*> m_items;
};

#endif // SHORTCUTCOLLECTION_H

shortcutcollection.cpp

#include "shortcutcollection.h"

ShortcutCollection::ShortcutCollection(QObject *parent):
    QObject(parent)
{
}

QQmlListProperty<ShortcutItem> ShortcutCollection::items()
{
    return QQmlListProperty<ShortcutItem>(this, m_items);
}

int ShortcutCollection::itemsCount() const
{
    return m_items.count();
}

ShortcutItem *ShortcutCollection::item(int index) const
{
    return m_items.at(index);
}

那你注册一下:

qmlRegisterType<ShortcutCollection>("FooModule", 1,0, "Shortcuts");
qmlRegisterType<ShortcutItem>("FooModule", 1,0, "Shortcut");

*.qml

import QtQuick 2.9
import QtQuick.Window 2.2
import FooModule 1.0 as U
Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    U.Shortcuts{
        U.Shortcut{

        }
        U.Shortcut{

        }
    }
}

您找到的完整示例here