Solved How to access std::vector<enum> in qml?
-
The TestMethod of c++ is called in qml.
c++
class Test : public QObject { Q_OBJECT public: enum TestEnum { RED = 0, BLACK = 0x1, YELLOW = 0x2, PINK = 0x4, }; Q_ENUM(TestEnum) Q_INVOKABLE std::vector<TestEnum> testMethod(); }; Q_DECLARE_METATYPE(std::vector<Test::TestEnum>)
main
qmlRegisterType<Test>("mytest.test",1,0,"Test"); qRegisterMetaType<std::vector<Test::TestEnum>>();
qml
import mytest.test 1.0 Test{ id: test } onClicked: { var result = test.testMethod(); }
It is return error.
qml:384: Error: Unknown method return type: std::vector<Test::TestEnum>.How do I can solve this problem?
-
Use QVariantList instead of vector.
-
@sierdzio thanks for your answer. QVariantList is works.
However,can't I use std::vector<TestEnum> in qml? -
@narae said in How to access std::vector<enum> in qml?:
@sierdzio thanks for your answer. QVariantList is works.
However,can't I use std::vector<TestEnum> in qml?No, QML engine does not know how to convert it to JavaScript.
-
@sierdzio thanks