1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#ifndef QMLUSING_H
#define QMLUSING_H
#include <QtQml/qqml.h>
#include <QtCore/qendian.h>
#include <QtCore/qobject.h>
template<typename T, typename tag>
struct TransparentWrapper
{
TransparentWrapper(T t_ = T()) : t(t_) {
static const bool registered = []() {
if (!QMetaType::registerConverter<TransparentWrapper, T>(toType)
|| !QMetaType::registerConverter<T, TransparentWrapper>(fromType)) {
return false;
}
// We always want int and double in addition because the engine uses those
if (!QMetaType::registerConverter<TransparentWrapper, double>(toType)
|| !QMetaType::registerConverter<double, TransparentWrapper>(fromType)) {
return false;
}
if constexpr (!std::is_same_v<T, int>) {
if (!QMetaType::registerConverter<TransparentWrapper, int>(toType)
|| !QMetaType::registerConverter<int, TransparentWrapper>(fromType)) {
return false;
}
}
return true;
}();
Q_ASSERT(registered);
}
static T toType(TransparentWrapper t) { return t.t; }
static TransparentWrapper fromType(T t) { return TransparentWrapper(t); }
T t;
operator T &() { return t; }
operator T() const { return t; }
TransparentWrapper &operator=(T t_)
{
t = t_;
return *this;
}
};
using myInt32 = TransparentWrapper<int32_t, struct int_tag>;
struct MyInt32Foreign
{
Q_GADGET
QML_FOREIGN(myInt32)
QML_USING(int32_t)
};
using myUInt32 = TransparentWrapper<uint32_t, struct int_tag>;
struct MyUInt32Foreign
{
Q_GADGET
QML_FOREIGN(myUInt32)
QML_USING(uint32_t)
};
class UsingUserValue
{
Q_GADGET
QML_VALUE_TYPE(usingUserValue)
Q_PROPERTY(myInt32 a READ a WRITE setA)
Q_PROPERTY(myUInt32 u READ u WRITE setU)
public:
myInt32 a() const { return m_a; }
void setA(myInt32 a) { m_a = a; }
myUInt32 u() const { return m_u; }
void setU(myUInt32 u) { m_u = u; }
Q_INVOKABLE myInt32 getB() const { return m_b; }
Q_INVOKABLE void setB(myInt32 b) { m_b = b; }
Q_INVOKABLE void setB(const QString &) { m_b = 99; }
private:
friend bool operator==(const UsingUserValue &lhs, const UsingUserValue &rhs)
{
return lhs.m_a == rhs.m_a && lhs.m_b == rhs.m_b && lhs.m_u == rhs.m_u;
}
friend bool operator!=(const UsingUserValue &lhs, const UsingUserValue &rhs)
{
return !(lhs == rhs);
}
myInt32 m_a = 24;
myInt32 m_b = 25;
myUInt32 m_u = 26;
};
class UsingUserObject : public QObject
{
Q_OBJECT
QML_ELEMENT
Q_PROPERTY(myInt32 a READ a WRITE setA NOTIFY aChanged)
Q_PROPERTY(myUInt32 u READ u WRITE setU NOTIFY uChanged)
Q_PROPERTY(UsingUserValue val READ val WRITE setVal NOTIFY valChanged)
public:
UsingUserObject(QObject *parent = nullptr) : QObject(parent) {}
myInt32 a() const { return m_a; }
void setA(myInt32 a)
{
if (a == m_a)
return;
m_a = a;
emit aChanged();
}
myUInt32 u() const { return m_u; }
void setU(myUInt32 u)
{
if (u == m_u)
return;
m_u = u;
emit uChanged();
}
UsingUserValue val() const { return m_val; }
void setVal(const UsingUserValue &val)
{
++m_setValCalls;
if (val == m_val)
return;
m_val = val;
emit valChanged();
}
Q_INVOKABLE myInt32 getB() const { return m_b; }
Q_INVOKABLE void setB(myInt32 b) { m_b = b; }
Q_INVOKABLE void setB(const QString &) { m_b = 101; }
int setValCalls() const { return m_setValCalls; }
signals:
void aChanged();
void uChanged();
void valChanged();
private:
int m_setValCalls = 0;
myInt32 m_a = 7;
myInt32 m_b = 5;
myUInt32 m_u = 9;
UsingUserValue m_val;
};
#endif // QMLUSING_H
|