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

How to create new instance of a Q_GADGET struct in QML?

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

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

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

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,但我想将其作为单个参数发送回插槽。

这里是 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中注册了这个类型:

qRegisterMetaType<BatteryState>();

建议?

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

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

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

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
    }