【问题标题】:Collection of QObjects like ListModel像 ListModel 这样的 QObject 集合
【发布时间】:2019-04-09 06:54:40
【问题描述】:

我想创建一个ShortcutItems 的列表作为QObject 并将ShortcutItems 添加到其中。例如,我有这个:

#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
}

但我想创建一个快捷方式列表(比如 ListModel 里面有 ListElements),如下所示:

U.Shortcuts {
    U.Shortcut {

    }
    U.Shortcut {

    }
}

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

【问题讨论】:

  • 您想对填充的U.Shortcuts 做什么?为什么不能使用ListModel 定义快捷方式的信息? list&lt;Action&gt; 怎么样?
  • @GrecKo 我需要继承并使用我自己的模型,这是一个 cpp 模型,据我所知,我不能使用 listmodel,关于第二部分,我不知道你对 List!

标签: qt qml qobject


【解决方案1】:

您必须创建一个 QObject,其中 Q_PROPERTY 作为 QQmlListProperty&lt;ShortcutItem&gt;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

【讨论】:

  • 是的,我正在寻找“QQmlListProperty”我如何搜索这种类型,在 java 和 c# 中搜索列表或集合,你会找到那些,Qt 和 c++ 中相同事物的文档是什么跨度>
  • @MoreMag 我不懂java、c#,但是doc.qt.io/qt-5/qtqml-referenceexamples-default-example.html中有一个例子
  • ty 好答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多