• 5905阅读
  • 2回复

[提问]QML中调用C++方法出现问题,求救 [复制链接]

上一主题 下一主题
离线cycloneii
 

只看楼主 倒序阅读 楼主  发表于: 2013-05-14
我从QObject派生了一个类InvokableObject, 内容如下
InvokableObject.h
  1. #ifndef INVOKEABLEOBJ_H
  2. #define INVOKEABLEOBJ_H
  3. #include <QObject>
  4. #include <QVariant>
  5. class InvokeableObj : public QObject
  6. {
  7.     Q_OBJECT
  8. public:
  9.     explicit InvokeableObj(QObject *parent = 0);
  10.     Q_INVOKABLE QVariant getOptionValue(QString key) const { return key;}
  11. signals:
  12.     
  13. public slots:
  14.     
  15. };
  16. #endif // INVOKEABLEOBJ_H
InvokableObject.cpp
  1. #include "invokeableobj.h"
  2. #include <QtDeclarative>
  3. InvokeableObj::InvokeableObj(QObject *parent) :
  4.     QObject(parent)
  5. {
  6. }
main.cpp
  1. #include <QApplication>
  2. #include <QtDeclarative>
  3. #include "invokeableobj.h"
  4. #include "qmlapplicationviewer.h"
  5. Q_DECL_EXPORT int main(int argc, char *argv[])
  6. {
  7.     QScopedPointer<QApplication> app(createApplication(argc, argv));
  8.     QmlApplicationViewer viewer;
  9.     viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
  10.     viewer.setMainQmlFile(QLatin1String("qml/testinvokable/main.qml"));
  11.     viewer.showExpanded();
  12.     viewer.rootContext()->setContextProperty("testobject", new InvokeableObj(&viewer));
  13.     return app->exec();
  14. }
main.qml
  1. // import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
  2. import QtQuick 1.1
  3. Rectangle {
  4.     width: 360
  5.     height: 360
  6.     Text {
  7.         id: text1
  8.         text: testobject.getOptionValue("ipaddress")
  9.         anchors.centerIn: parent
  10.     }
  11.     MouseArea {
  12.         anchors.fill: parent
  13.         onClicked: {
  14.             Qt.quit();
  15.         }
  16.     }
  17. }
可以在qml中调用InvokableObject的getOptionValue方法
但是如果将InvokableObject的基类改成QDeclarativePropertyMap,那么就会报错,TypeError: Result of expression 'testobject.getOptionValue' [undefined] is not a function.

为什么QObject做基类,工作正常,从QDeclarativePropertyMap派生就不能调用了getOptionValue方法了呢?





离线cavendish

只看该作者 1楼 发表于: 2013-05-14
http://doc-snapshot.qt-project.org/4.8/qml-extending.html

QML relies heavily on Qt's meta object system and can only instantiate classes that derive from QObject. For visual element types, this will usually mean a subclass of QDeclarativeItem; for models used with the view elements, a subclass of QAbstractItemModel; and for arbitrary objects with properties, a direct subclass of QObject.
离线cycloneii

只看该作者 2楼 发表于: 2013-05-15
引用第1楼cavendish于2013-05-14 23:56发表的  :
http://doc-snapshot.qt-project.org/4.8/qml-extending.html
QML relies heavily on Qt's meta object system and can only instantiate classes that derive from QObject. For visual element types, this will usually mean a subclass of QDeclarativeItem; for models used with the view elements, a subclass of QAbstractItemModel; and for arbitrary objects with properties, a direct subclass of QObject.

并不一定要是QObject的直接子类,比如如果InvokableObject从QSettings派生,那么qml依然可以调用getOptionValue方法,但是换成QDeclarativePropertyMap就不行。其中应该还有其他的原因吧
快速回复
限100 字节
 
上一个 下一个