如何在QML Javascript中创建和使用C ++对象 [英] How to create and use C++ objects in QML Javascript

查看:250
本文介绍了如何在QML Javascript中创建和使用C ++对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序同时使用c ++和QML。

My app uses both c++ and QML.

我已经在C ++部分定义了几个对象来访问SQL等。

I've defined several objects in C++ part to access SQL etc.

它看起来像:

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");

理想情况下,我只需要在JML中使用这些对象而不是QML。

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.

所以我的问题:


  • 如何在Javascript中实例化C ++中定义的对象?我试过 var obj = Qt.createComponent(MyObject); 但似乎不行。是否可以在正常的JS样式中定义新对象 - var obj = new MyObject;

  • 如何访问此创建的对象在JavaScript中?我尝试了obj.someFunction(xxx)但得到了一些错误 - TypeError:对象QQmlComponent(0x3605f5c0)的属性'someFunction'不是函数。我在这里做错了什么?我的对象派生自QObject,而不是来自QQmlComponent。

  • 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.

推荐答案

你的对象不是 Component ,但你可以使用 Qt.createQmlObject

Your object isn't a Component, but you can use Qt.createQmlObject instead.

这篇关于如何在QML Javascript中创建和使用C ++对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆