How to create and use C ++ objects in QML Javascript
source link: https://www.codesd.com/item/how-to-create-and-use-c-objects-in-qml-javascript.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to create and use C ++ objects in QML Javascript
My app uses both c++ and QML.
I've defined several objects in C++ part to access SQL etc.
It looks like:
class MyObject : public QObject
{
Q_OBJECT
public:
MyObject(QObject *parent = 0);
Q_INVOKABLE void someFunction(const QString &query);
};
qmlRegisterType<MyObject>("xxx.xxx", 1, 0, "MyObject");
Ideally, I need to use these objects only in Javascript not in QML.
I tried a lot of examples and read all the documentation but still can't solve my problem.
So my questions:
- How can I instance in Javascript an object defined in C++? I tried
var obj = Qt.createComponent("MyObject");
but it seems not works. Is it possible to define new object in normal JS style -var obj = new MyObject;
? - How can I access this created object in javascript? I tried obj.someFunction("xxx") but got some error -
TypeError: Property 'someFunction' of object QQmlComponent(0x3605f5c0) is not a function.
What I do wrong here? My object derived from QObject, not from QQmlComponent.
Your object isn't a Component
, but you can use Qt.createQmlObject
instead.
Related Articles
How to create and use an object of another class in Java
How to create and use an object with Objective-C?
How to create and use a C ++ static library for the ios application
How to create and use C # dll files
How to create and use LESS models?
Delphi: How to create and use Thread locally?
How to sum values using an object? In JavaScript
How to create and use self-hosted objects to share custom stories on Android?
Is the bad / inefficient way to create and use PDO objects in php?
How to create and use ffmpeg in Android
How to create and use UDF on cloud datalab?
How to create and use a 1D layered texture in CUDA
how to create and use a self-defined shared library with gcc?
How to declare and use the object in this build.sbt?
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK