0

I wrote a C++ plugin that exposes a C++ MyCppClass for QML. The Q_INVOKABLE functions work, but I cannot "see" any Q_PROPERTY properties for the same instance.

Assume a trivial MyCppClass:

class MyCppClass : public QObject {
  Q_OBJECT
  Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
public:
  MyCppClass();
  virtual ~MyCppClass();
  Q_INVOKABLE void myInvokeFunc();
  int value();
  void setValue(int i);
signals:
  void valueChanged(int);
private:
  int m_iValue;  
};

Example QML use:

// FILE: MyQml.qml
import QtQuick 2.1
import QtQuick.Controls 1.0
import QtQuick.Window 2.0

import MyCppPlugin 1.0

Item {
  MyCppClass {      //<==OK
     id: myClass    //<==OK
     value: 42      //<==QML LOAD ERROR
  }

  Button {
    text: "Hello"
    onClicked: {
      myClass.myInvokeFunc()   //<==OK
    }
}

What's going on:

  • SUCCESS: C++ Plugin loads through QML "import"
  • SUCCESS: Instantiating a C++ object in QML
  • SUCCESS: Calling Q_INVOKABLE function from QML
  • FAIL: Accessing any properties in QML from that same C++ object (QML fails to load, error: "Cannot assign to non-existent property "value"") .... assigning/binding fails to load the QML file, and "reading" any property results in "undefined")

This doesn't make sense to me (Qt 5.1.1, Win7). The plugin loads, the C++ object is instantiated, the Q_INVOKABLE function works, but I apparently cannot "see" any Q_PROPERTY properties.

QUESTION: What are the possible scenarios where Q_INVOKABLE works on an instance, but Q_PROPERTY properties are still unavailable for that same instance?

4

2 回答 2

0

我也使用 Win 7 Qt 5.1.1,我对 Q_PROPERTY 没有任何问题。

也许你的函数实现有问题(比如改变信号的无限循环)?或者你有多个版本的插件并使用了错误的?

如果问题不是这样,您可以尝试在 C++ 中创建 MyCppClass 对象并使用元对象函数(特别是 QObject::dynamicPropertyNames 或 QMetaObject::propertyCount/property)探索它以获取属性列表

于 2013-09-25T18:05:14.177 回答
0

很可能,您没有在 Qt 元对象系统中正确注册您的类。它通常通过qmlRegisterType或类似的方法完成。

于 2016-03-25T11:37:56.607 回答