小编Eli*_*kis的帖子

QList <T *>到QML数组

我有一个从QObject继承的Bar类。我想在类Foo中使用Bar指针的QList,并在qml中公开它。

foo.h

#ifndef FOO_H
#define FOO_H

#include <QQuickItem>

class Bar : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int x READ x)

public:
    explicit Bar(QObject *parent = nullptr)
        : QObject(parent), mX(123) {}
    virtual ~Bar() {}

    int x() const { return mX; }
private:
    int mX;
};

class Foo : public QQuickItem
{
    Q_OBJECT
    Q_PROPERTY(QList<QObject *> t1 READ t1)
    Q_PROPERTY(QList<Bar *> t2 READ t2)

public:
    explicit Foo(QQuickItem *parent = nullptr)
        : QQuickItem(parent) {
        mT1.append(new Bar(this));
        mT1.append(new Bar(this));

        mT2.append(new Bar(this));
        mT2.append(new Bar(this));
    }
    virtual ~Foo() …
Run Code Online (Sandbox Code Playgroud)

c++ qt qml

2
推荐指数
2
解决办法
3516
查看次数

标签 统计

c++ ×1

qml ×1

qt ×1