How to send javascript object to C++ from qml? - Stack Overflow

I have a method in C++ that expects an object:Q_INVOKABLE void sendValue (const MyClass &value);I&#

I have a method in C++ that expects an object:

Q_INVOKABLE void sendValue (const MyClass &value);

I'd like to call this method from qml, inside a javascript function, like this:

MyApi.sendValue({
    "one":"one",
    "two":2.0,
    "three": false,
    "four": [ 5, 6 ],
    }
});

MyClass is defined as follows:

#ifndef MYCLASS_H
#define MYCLASS_H
#include <QString>
#include <QVariant>
class MyClass {
    QString one;
    double two;
    bool three;
    int four[10];
public:
    MyClass();
    ~MyClass();
    // getters, setters, etc.
};
Q_DECLARE_METATYPE(MyClass)
#endif // MYCLASS_H

In main.cpp, MyClass is registered with qRegisterMetaType<MyClass>(); But none of the setters gets called, only MyClass' default constructor.

I have a method in C++ that expects an object:

Q_INVOKABLE void sendValue (const MyClass &value);

I'd like to call this method from qml, inside a javascript function, like this:

MyApi.sendValue({
    "one":"one",
    "two":2.0,
    "three": false,
    "four": [ 5, 6 ],
    }
});

MyClass is defined as follows:

#ifndef MYCLASS_H
#define MYCLASS_H
#include <QString>
#include <QVariant>
class MyClass {
    QString one;
    double two;
    bool three;
    int four[10];
public:
    MyClass();
    ~MyClass();
    // getters, setters, etc.
};
Q_DECLARE_METATYPE(MyClass)
#endif // MYCLASS_H

In main.cpp, MyClass is registered with qRegisterMetaType<MyClass>(); But none of the setters gets called, only MyClass' default constructor.

Share Improve this question asked Jan 27, 2015 at 21:19 kazmerkazmer 5215 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You can send javascript objects to c++ from qml via QVariantMap and javascript array with QVariantList. It also goes the other way, you can send javascript object to qml using QVariantMap from c++. Just make your function in c++ Q_INVOKABLE or a slot and have the parameter be QVariantMap, and convert that QVariantMap into MyClass {}.

See http://doc.qt.io/qt-5/qtqml-cppintegration-data.html for details (search for QVariantList and QVariantMap to JavaScript Array and Object).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742349470a4427219.html

相关推荐

  • How to send javascript object to C++ from qml? - Stack Overflow

    I have a method in C++ that expects an object:Q_INVOKABLE void sendValue (const MyClass &value);I&#

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信