简体   繁体   English

如何通过 QVector<custom_q_gadget> 到 QML?</custom_q_gadget>

[英]How to pass QVector<Custom_Q_GADGET> to QML?

I have QObject class with 2 properties:我有具有 2 个属性的 QObject class:

class Foo : public QObject {
Q_OBJECT
    Q_PROPERTY(QVector<Namespace1::Namespace2::Item> items READ getItemVector)
    Q_PROPERTY(QVector<QString> tests READ getTestVector)
    ...
}

QVector<Namespace1::Namespace2::Item> Foo::getItemVector() const
{
    return _item_vector;
}

QVector<QString> Foo::getTestVector() const
{
    return _test_vector;
}

Everything is ok with QVector of QString s in the QML, but I was not able to pass QVector of Namespace1::Namespace2::Item s. QVectorQString的 QVector 一切正常,但我无法通过Namespace1::Namespace2::ItemQVector

Output to QML console gives me: QVariant(QVector<Namespace1::Namespace2:Item>) for console.log(foo.items) and length = undefined for console.log(foo.items.length) Output 到 QML 控制台给了我: QVariant(QVector<Namespace1::Namespace2:Item>) for console.log(foo.items) 和length = undefined for console.log(foo.items.length)

What I have found out from the Qt documentation:我从 Qt 文档中发现的内容:

These sequence types are implemented directly in terms of the underlying C++ sequence.这些序列类型是根据底层 C++ 序列直接实现的。 There are two ways in which such sequences can be exposed to QML: as a Q_PROPERTY of the given sequence type;有两种方式可以将此类序列暴露给 QML: 作为给定序列类型的 Q_PROPERTY; or as the return type of a Q_INVOKABLE method.或作为 Q_INVOKABLE 方法的返回类型。

... ...

In particular, QML currently supports: ... QList< QString >... and all registered QList, QVector, QQueue, QStack, QSet, QLinkedList, std::list, std::vector that contain a type marked with Q_DECLARE_METATYPE.特别是,QML 当前支持: ... QList< QString >... 以及所有已注册的 QList、QVector、QQueue、QStack、QSet、QLinkedList、std::list、std::vector,它们包含用 Q_DECLARE_METATYPE 标记的类型。

As I understand, everything boils down simply to:据我了解,一切都归结为:

Q_DECLARE_METATYPE(Namespace1::Namespace2::Item)

Besides this:除此以外:

Some types are registered automatically and do not need this macro: Pointers to classes derived from QObject, QList< T >, QVector< T > , QQueue< T >, QStack< T >, QSet< T > or QLinkedList where T is a registered meta type某些类型是自动注册的,不需要此宏:指向派生自 QObject、QList< T >、 QVector< T > 、QQueue< T >、QStack< T >、QSet< T > 或 QLinkedList 的类的指针,其中 T 是已注册的元类型

However, I also tried to declare metatype(without luck, obviously):但是,我也尝试声明元类型(显然没有运气):

Q_DECLARE_METATYPE(QVector<Namespace1::Namespace2::Item>)

and also tried to register metatypes(without any luck as well):并且还尝试注册元类型(也没有任何运气):

qRegisterMetaType<Namespace1::Namespace2::Item>("Namespace1::Namespace2::Item");
qRegisterMetaType<QVector<Namespace1::Namespace2::Item>>("QVector<Namespace1::Namespace2::Item>");

The Item by itself is a Q_GADGET : Item本身就是一个Q_GADGET

namespace Namespace1 { namespace Namespace2 {
    class Item
    {
    Q_GADGET
        Q_PROPERTY(QString name READ getName CONSTANT)

    public:
        Item();
        Item(const QString& name);
        Item(const Item& origin);
        Item(Item&& origin);
        ~Item();

    private:
        QString _name;

    public:
        Item& operator=(const Item& rhs);
        Item& operator=(Item&& rhs);

    friend QDataStream& operator<<(QDataStream& out, const Item& item);
    friend QDataStream& operator>>(QDataStream& in, Item& item);
    friend QDebug operator<<(QDebug debug, const Item& item);
    }
} }

What am I missing?我错过了什么? Thank you!谢谢!

The "traditional" way of doing this is to convert each element to a QVariant, and pass collection as a QVariantList这样做的“传统”方式是将每个元素转换为 QVariant,并将集合作为 QVariantList 传递

QVariantList MyClass::getFooCollection(void) const
{
    QVariantList list;

    for (const auto& l: fooCollection_)
    {
        list.append(QVariant::fromValue(l));
    }

    return list;
}

The metatype system declaration and registration you have shown are required您显示的元类型系统声明和注册是必需的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM