2022-05-13 13:12:05 +00:00
|
|
|
// Copyright (C) 2022 The Qt Company Ltd.
|
2024-02-22 14:51:16 +00:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
|
2021-11-23 18:00:29 +00:00
|
|
|
|
|
|
|
#ifndef OBJECTWITHMETOD_H
|
|
|
|
#define OBJECTWITHMETOD_H
|
|
|
|
|
|
|
|
#include <QtCore/qobject.h>
|
|
|
|
#include <QtCore/qproperty.h>
|
|
|
|
#include <QtQml/qqml.h>
|
2023-02-27 15:27:13 +00:00
|
|
|
#include <QtQml/private/qv4engine_p.h>
|
2021-11-23 18:00:29 +00:00
|
|
|
|
|
|
|
// Make objectName available. It doesn't exist on the builtin QtObject type
|
|
|
|
struct QObjectForeignForObjectName {
|
|
|
|
Q_GADGET
|
|
|
|
QML_FOREIGN(QObject)
|
|
|
|
QML_ANONYMOUS
|
|
|
|
};
|
|
|
|
|
|
|
|
class ObjectWithMethod : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
QML_ELEMENT
|
|
|
|
Q_PROPERTY(int fff MEMBER theThing BINDABLE theThingBindable FINAL)
|
|
|
|
|
|
|
|
public:
|
|
|
|
ObjectWithMethod(QObject *parent = nullptr) : QObject(parent) { theThing = 5; }
|
|
|
|
|
|
|
|
Q_INVOKABLE int doThing() const { return theThing; }
|
|
|
|
QProperty<int> theThing;
|
|
|
|
QBindable<int> theThingBindable() { return QBindable<int>(&theThing); }
|
2023-02-27 15:27:13 +00:00
|
|
|
|
2024-02-26 11:44:21 +00:00
|
|
|
// The meta methods are populated back to front.
|
|
|
|
// The V4Function flag should not bleed into the others in either case.
|
|
|
|
|
2024-04-11 08:17:52 +00:00
|
|
|
Q_INVOKABLE void overloaded(QQmlV4FunctionPtr) { setObjectName(QStringLiteral("javaScript")); }
|
2023-02-27 15:27:13 +00:00
|
|
|
Q_INVOKABLE void overloaded(double) { setObjectName(QStringLiteral("double")); }
|
|
|
|
Q_INVOKABLE void overloaded(const QString &) { setObjectName(QStringLiteral("string")); }
|
2023-10-17 12:06:32 +00:00
|
|
|
|
|
|
|
Q_INVOKABLE void foo(const QString &bla) { setObjectName(bla); }
|
|
|
|
Q_INVOKABLE void foo(ObjectWithMethod *) { setObjectName(QStringLiteral("ObjectWithMethod")); }
|
2024-02-26 11:44:21 +00:00
|
|
|
|
|
|
|
Q_INVOKABLE void overloaded2(double) { setObjectName(QStringLiteral("double")); }
|
|
|
|
Q_INVOKABLE void overloaded2(const QString &) { setObjectName(QStringLiteral("string")); }
|
2024-04-11 08:17:52 +00:00
|
|
|
Q_INVOKABLE void overloaded2(QQmlV4FunctionPtr) { setObjectName(QStringLiteral("javaScript")); }
|
2021-11-23 18:00:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class OverriddenObjectName : public ObjectWithMethod
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY(QString objectName READ objectName WRITE setObjectName BINDABLE objectNameBindable)
|
|
|
|
|
|
|
|
// This shouldn't work
|
|
|
|
Q_PROPERTY(int fff READ fff BINDABLE nothingBindable)
|
|
|
|
|
|
|
|
public:
|
|
|
|
OverriddenObjectName(QObject *parent = nullptr) : ObjectWithMethod(parent)
|
|
|
|
{
|
2022-03-21 09:21:18 +00:00
|
|
|
m_objectName = QStringLiteral("borschtsch");
|
2021-11-23 18:00:29 +00:00
|
|
|
nothing = 77;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString objectName() const { return m_objectName.value(); }
|
|
|
|
void setObjectName(const QString &objectName) { m_objectName.setValue(objectName); }
|
|
|
|
QBindable<QString> objectNameBindable() { return QBindable<QString>(&m_objectName); }
|
2022-03-21 09:21:18 +00:00
|
|
|
Q_INVOKABLE QString doThing() const { return QStringLiteral("7"); }
|
2021-11-23 18:00:29 +00:00
|
|
|
|
|
|
|
int fff() const { return nothing.value(); }
|
|
|
|
QBindable<int> nothingBindable() { return QBindable<int>(¬hing); }
|
|
|
|
private:
|
|
|
|
QProperty<int> nothing;
|
|
|
|
QProperty<QString> m_objectName;
|
|
|
|
};
|
|
|
|
|
2023-07-24 19:15:25 +00:00
|
|
|
class ObjectWithStringListMethod : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
QML_ELEMENT
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ObjectWithStringListMethod(QObject *parent = nullptr) : QObject(parent)
|
|
|
|
{
|
2024-10-09 07:41:43 +00:00
|
|
|
m_names.append(QStringLiteral("One"));
|
|
|
|
m_names.append(QStringLiteral("Two"));
|
2023-07-24 19:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Q_INVOKABLE QStringList names() const { return m_names; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
QStringList m_names;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ObjectFactory : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
QML_ELEMENT
|
|
|
|
QML_SINGLETON
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ObjectFactory(QObject *parent = nullptr) : QObject(parent) {}
|
|
|
|
Q_INVOKABLE ObjectWithStringListMethod *getFoo()
|
|
|
|
{
|
|
|
|
if (!m_foo)
|
|
|
|
m_foo = new ObjectWithStringListMethod(this);
|
|
|
|
return m_foo;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ObjectWithStringListMethod *m_foo = nullptr;
|
|
|
|
};
|
|
|
|
|
2021-11-23 18:00:29 +00:00
|
|
|
#endif // OBJECTWITHMETHOD_H
|