I am writing scripting wrappers for my SW so I can control my SW via scripts. The purpose of the wrapper classes is to separate the scripting interface from the actual class because I might not want to expose all of the signals and slots of the class. I'm using Qt 5.13 and QJSEngine.
My issue is that based on what I've read and what I've experimented it seems that to be able to expose an enum it needs to be within a class that inherits QObject and is exposed via newQObject()/setProperty(). However I do not want to expose the class Foo in the following example, but I still want to expose the enum Foo::Bar to the scripting environment. How do I do this? Q_ENUM seems to assume the enum is within a QObject and that the QObject is exposed (property) in the scripting environment. Here is a short example what I am doing (I want to be able to call FooWrapper::slot1() from the scripting environment):
class Foo : public QObject
{
Q_OBJECT
public:
Foo(QJSEngine& engine)
{
auto wrapper = new FooWrapper(this, engine);
}
enum class Bar
{
VAL1,
VAL2
};
public slots:
void slot1(Bar bar);
void slot2();
};
class FooWrapper : public QObject
{
Q_OBJECT
public:
Foo(Foo& foo, QJSEngine& engine)
: foo(&foo)
{
QJSValue obj = engine.newQObject(this);
auto gObj = engine.globalObject();
gObj.setProperty("foo", obj);
}
public slots:
void slot1(Bar bar)
{
foo->slot1(bar);
}
private:
Foo* foo;
};