QML一点点注意的地方

来源:互联网 发布:全球数据公司 编辑:程序博客网 时间:2024/05/17 21:58

1、id需要小写

2、模块名首字母要大写

3、由原来的import Qt 4.7 修改为了import QtQuick 1.0

4、states是:[]

5、国际化用:qsTr()

6、导入js文件用:import *.js as Name

7、属性的设置声明方法:property int len 12,别名:property alias cellColor: rectangle.color/

      cpp中是:Q_PROPERTY(int hour READ hour WRITE setHour NOTIFY timeChanged)

8、信号声明:signal clicked(color cellColor),处理为onClincked

9、C++扩展组件继承:QObject

      注册组件 :qmlRegisterType<ClassName>(uri,1, 0,"Class");  这样需要qml导入声明,也可以下面这样,qml可以直接使用

          MyClass myObj;          QDeclarativeEngine *engine=viewer.engine();          QDeclarativeContext *context=engine->rootContext();          context->setContextProperty("myObjectExposeByCXProperty", &myObj);

      声明接口 :Q_INTERFACES(PluginInterface)

      导出接口:Q_EXPORT_PLUGIN2(exampleplugins, YourPlugin)

      注册语句应该在载入使用这个接口的qml文件之前

10、cpp函数被QML直接调用的方法:Q_INVOKABLE和public slots

11、cpp获取qml的函数

       QDeclarativeComponent component(&engine, "MyItem.qml");       QObject *object = component.create();       QMetaObject::invokeMethod(object, "myQmlFunction",Q_RETURN_ARG(QVariant, returnedValue),Q_ARG(QVariant, arg));