Invokable constructor in QT

In QT 5.15 I declare a class with an invokable constructor:

class TestClass : public QObject
{
    Q_OBJECT

public:

    Q_INVOKABLE TestClass(QString val) {}
};

Q_DECLARE_METATYPE(TestClass*)

register its type:

qRegisterMetaType<TestClass*>();

and then create it dynamically with a code like this:

int id = QMetaType::type("TestClass*");

auto metaObject = QMetaType::metaObjectForType(id);

QObject* instance = metaObject->newInstance(Q_ARG(QString, "abc"));

Links:

Leave a Reply

Your email address will not be published. Required fields are marked *