如何在 QML 中创建 Q_GADGET 结构的新实例?

2022-01-19 00:00:00 qt struct qml signals-slots c++

我可以使用带有 Q_GADGET 标记的结构从 C++ 到 QML 发出信号.

I can emit signals with structs tagged with Q_GADGET from C++ to QML.

是否可以将这样的结构从 QML 发送到 C++ 插槽?我的代码在第一步失败:在 QML 中创建一个实例.

Is it possible send such a struct from QML to a C++ slot? My code fails on the first step: creating an instance in QML.

此代码在第一行失败...

This code fails on the first line ...

var bs = new BatteryState()
bs.percentRemaining = 1.0
bs.chargeDate = new Date()
DataProvider.setBatteryState(bs)

...有错误:

qrc:///main.qml:34: ReferenceError: BatteryState is not defined

我可以将 BatteryStatus 结构从 C++ 发送到 QML,但我想将一个作为单个参数发送回插槽.

I can emit a BatteryStatus struct from C++ to QML, but I would like to send one back as a single parameter to a slot.

这是 BatteryState.h &BatteryState.cpp:

Here is BatteryState.h & BatteryState.cpp:

// BatteryState.h
#pragma once

#include <QDate>
#include <QMetaType>

struct BatteryState
{
    Q_GADGET
    Q_PROPERTY(float percentRemaining  MEMBER percentRemaining)
    Q_PROPERTY(QDate date              MEMBER date)

public:
    explicit BatteryState();
    BatteryState(const BatteryState& other);
    virtual ~BatteryState();
    BatteryState& operator=(const BatteryState& other);
    bool operator!=(const BatteryState& other) const;
    bool operator==(const BatteryState& other) const;

    float percentRemaining;
    QDate date;
};
Q_DECLARE_METATYPE(BatteryState)

// BatteryState.cpp
#include "BatteryState.h"

BatteryState::BatteryState()
    : percentRemaining(), date(QDate::currentDate())
{}

BatteryState::BatteryState(const BatteryState& other)
    : percentRemaining(other.percentRemaining),
      date(other.date)
{}

BatteryState::~BatteryState() {}

BatteryState&BatteryState::operator=(const BatteryState& other)
{
    percentRemaining = other.percentRemaining;
    date = other.date;
    return *this;
}

bool BatteryState::operator!=(const BatteryState& other) const {
    return (percentRemaining != other.percentRemaining
            || date != other.date);
}

bool BatteryState::operator==(const BatteryState& other) const {
    return !(*this != other);
}

我在main.cpp中注册了这个类型:

I register this type in main.cpp:

qRegisterMetaType<BatteryState>();

建议?

推荐答案

你不要在 QML 中创建 Q_GADGET,QML 对象需要是 QObject 派生的,并且不是通过 new 创建的 - 仅适用于 JS 对象.小工具只是生成元数据,以便您可以从 QML 访问它们的成员等并传递,仅此而已.

You don't create Q_GADGETs in QML, QML objects need to be QObject derived, and are not created via new - that's for JS objects only. Gadgets just generate meta data so that you can access their members and such from QML and pass around, that's about it.

是否可以将这样的结构从 QML 发送到 C++ 插槽?

Is it possible send such a struct from QML to a C++ slot?

可以发送,但不会在 QML 中创建.它可以从 C++ 函数返回给 QML,也可以作为某个对象的属性公开.

It is possible to send, but it would not be created in QML. It could be returned to QML from a C++ function or be exposed as a property of some object.

struct Test {
    Q_GADGET
    Q_PROPERTY(int test MEMBER test)
  public:
    Test() : test(qrand()) {}
    int test;
    Q_SLOT void foo() { qDebug() << "foo"; }
};

class F : public QObject { // factory exposed as context property F
    Q_OBJECT
  public slots:
    Test create() { return Test(); }
    void use(Test t) { qDebug() << t.test; }
};


    // from QML
    property var tt: F.create()

    Component.onCompleted: {
      F.use(F.create()) // works
      var t = F.create()
      console.log(t.test) // works
      F.use(t) // works
      console.log(tt.test) // works
      F.use(tt) // works
      tt.test = 555
      F.use(tt) // works
      t.test = 666
      F.use(t) // works
      t.foo() // works
    }

相关文章