在C++程序中使用QML.doc_第1页
在C++程序中使用QML.doc_第2页
在C++程序中使用QML.doc_第3页
在C++程序中使用QML.doc_第4页
在C++程序中使用QML.doc_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

在C+程序中使用QMLQML API是分为三个主类QDeclarativeEngine, QdeclarativeComponent 与 QDeclarativeContext。QDeclarativeEngine 提供QML运行的环境,QdeclarativeComponent 封装了QML Documents 与QDeclarativeContext允许程序导出数据到QML组件实例。QML还包含了API的一个方便,通过QDeclarativeView 应用程序只需要简单嵌入QML组件到一个新的QGraphicsView就可以了。这有许多细节将在下面讨论。QDeclarativeView 主要是用于快速成型的应用程序里。如果你是重新改进使用QML的Qt应用程序,请参阅 整合QML到现有的Qt UI代码。基本用法每个应用程序至少需求一个QDeclarativeEngine。QDeclarativeEngine允许配置全局设置应用到所有的QML组件实例中,例如QNetworkAccessManager是用于网络通信以及永久储存的路径。如果应用程序需求在QML组件实例间需求不同的设置只需要多个QDeclarativeEngine。使用QDeclarativeComponent类载入QML Documents。每个QDeclarativeComponent实例呈现单一QML文档。QDeclarativeComponent可以传递一个文档的地址或文档的原始文本内容。该文档的URL可以是本地文件系统的地址或通过QNetworkAccessManager支持的网络地址。QML组件实例通过调用QDeclarativeComponent:create()模式来创建。在这里载入一个QML文档的示例并且从它这里创建一个对象。QDeclarativeEngine *engine = new QDeclarativeEngine(parent);QDeclarativeComponent component(engine, QUrl:fromLocalFile(“main.qml”);QObject *myObject = component.create();导出数据QML组件是以QDeclarativeContext实例化的。context允许应用程序导出数据到该QML组件实例中。单个QDeclarativeContext 可用于一应用程序的所有实例对象或针对每个实例使用QDeclarativeContext 可以创建更为精确的控制导出数据。如果不传递一个context给QDeclarativeComponent:create()模式;那么将使用QDeclarativeEngine的root context。数据导出通过该root context对所有对象实例是有效的。简单数据为了导出数据到一个QML组件实例,应用程序设置Context属性;然后由QML属性绑定的名称与JavaScrip访问。下面的例子显示通过QGraphicsView如何导出一个背景颜色到QML文件中:/main.cpp#include #include #include int main(int argc, char *argv)QApplication app(argc, argv);QDeclarativeView view;QDeclarativeContext *context = view.rootContext();context-setContextProperty(“backgroundColor”,QColor(Qt:yellow);view.setSource(QUrl:fromLocalFile(“main.qml”);view.show();return app.exec();/main.qmlimport Qt 4.7Rectangle width: 300height: 300color: backgroundColorText anchors.centerIn: parenttext: “Hello Yellow World!”或者,如果你需要main.cpp不需要在QDeclarativeView显示创建的组件,你就需要使用QDeclarativeEngine:rootContext()替代创建QDeclarativeContext实例。QDeclarativeEngine engine;QDeclarativeContext *windowContext = new QDeclarativeContext(engine.rootContext();windowContext-setContextProperty(“backgroundColor”, QColor(Qt:yellow);QDeclarativeComponent component(&engine, “main.qml”);QObject *window = component.create(windowContext);Context属性的操作像QML绑定的标准属性那样在这个例子中的backgroundColor Context属性改变为红色;那么该组件对象实例将自动更新。注意:删除任意QDeclarativeContext的构造是创建者的事情。当window组件实例撤消时不再需要windowContext时,windowContext必须被消毁。最简单的方法是确保它设置window作为windowContext的父级。QDeclarativeContexts 是树形结构除了root context每个QDeclarativeContexts都有一个父级。子级QDeclarativeContexts有效的继承它们父级的context属性。这使应用程序分隔不同数据导出到不同的QML对象实例有更多自由性。如果QDeclarativeContext设置一context属性,同样它父级也被影响,新的context属性是父级的影子。如下例子中,background context属性是Context 1,也是root context里background context属性的影子。结构化数据context属性同样可用于输出结构化与写数据到QML对象。除了QVariant支持所有已经存在的类型外,QObject 派生类型可以分配给context属性。 QObject context属性允许数据结构化输出并允许QML来设置值。下例创建CustomPalette对象并设置它作为palette context属性。class CustomPalette : public QObjectQ_OBJECTQ_PROPERTY(QColor background READ background WRITE setBackground NOTIFY backgroundChanged)Q_PROPERTY(QColor text READ text WRITE setText NOTIFY textChanged)public:CustomPalette() : m_background(Qt:white), m_text(Qt:black) QColor background() const return m_background; void setBackground(const QColor &c) if (c != m_background) m_background = c;emit backgroundChanged();QColor text() const return m_text; void setText(const QColor &c) if (c != m_text) m_text = c;emit textChanged();signals:void textChanged();void backgroundChanged();private:QColor m_background;QColor m_text;int main(int argc, char *argv)QApplication app(argc, argv);QDeclarativeView view;view.rootContext()-setContextProperty(“palette”, new CustomPalette);view.setSource(QUrl:fromLocalFile(“main.qml”);view.show();return app.exec();QML引用palette对象以及它的属性,为了设置背景与文本的颜色,这里是当单击窗口时,面板的文本颜色将改变成蓝色。import Qt 4.7Rectangle width: 240height: 320color: palette.backgroundText anchors.centerIn: parentcolor: palette.texttext: “Click me to change color!”MouseArea anchors.fill: parentonClicked: palette.text = “blue”;可以检测一个C+属性值这种情况下的CustomPalette的文本属性改变,该属性必须有相应的NOTIFY信息。NOTIFY信号是属性值改变时将指定一个信号发射。实现者应该注意的是,只有值改变时才发射信号,以防止发生死循环。访问一个绑定的属性,没有NOTIFY信号的话,将导致QML在运行时发出警告信息。动态结构化数据如果应用程序对结构化过于动态编译QObject类型;那么对动态结构化数据可在运行时使用QDeclarativePropertyMap 类构造。从QML调用 C+通过public slots输出模式或Q_INVOKABLE标记模式使它可以调用QObject派生出的类型。C+模式同样可以有参数并且可以返回值。QML支持如下类型:boolunsigned int, intfloat, double, qrealQStringQUrlQColorQDate,QTime,QDateTimeQPoint,QPointFQSize,QSizeFQRect,QRectFQVariant下面例子演示了,当MouseArea单击时控制“Stopwatch”对象的开关。/main.cppclass Stopwatch : public QObjectQ_OBJECTpublic:Stopwatch();Q_INVOKABLE bool isRunning() const;public slots:void start();void stop();private:bool m_running;int main(int argc, char *argv)QApplication app(argc, argv);QDeclarativeView view;view.rootContext()-setContextProperty(“stopwatch”,new Stopwatch);view.setSource(QUrl:fromLocalFile(“main.qml”);view.show();return app.exec();/main.qmlimport Qt 4.7Rectangle width: 300height: 300MouseArea anchors.fill: parentonClicked: if (stopwatch.isRunning()stopwatch.stop()elsestopwatch.start();值得注意的是,在这个特殊的例子里有更好的方法来达到同样的效果,在main.qml有”running”属性,这将会是一个非常优秀的QML代码:/ main.qmlimport Qt 4.7Rectangle MouseArea anchors.fill: parentonClicked: stopwatch.running = !stopwatch.running当然,它同样可以调用 functions declared in QML from C+。网络组件如果URL传递给QDeclarativeComponent是一网络资源或者QML文档引用一网络资源,QDeclarativeComponent要先获取网络数据;然后才可以创建对象。在这种情况下QDeclarativeComponent将有Loading status。直到组件调用QDeclarativeComponent:create()之前,应用程序将一直等待。下面的例子显示如何从一个网络资源载入QML文件。在创建QDeclarativeComponent之后,它测试组件是否加载。如果是,它连接QDeclarativeComponent:statusChanged()信号,否则直接调用continueLoading()。这个测试是必要的,甚至URL都可以是远程的,只是在这种情况下要防组件是被缓存的。MyApplication:MyApplication()/ component = new QDeclarativeComponent(engine, QUrl(“/main.qml”);if (component-isLoading()QObject:connect(component, SIGNAL(statusChanged(QDeclarativeComponent:Status),this, SLOT(continueLoading();elsecontinueLoading();void MyApplication:continueLoading()if (component-isError() qWarning() errors(); else QObject *myObject = component-create();Qt资源QML的内容可以使用qrc:URL方案从Qt 资源系统载入。例如:project/example.qrcmain.qmlimages/background.pngproject/QT += declarativeSOURCES += main.cppRESOURCES += example.qrcproject/main.cppint main(int argc, char *argv)QApplication app(argc, argv);QDeclarativeView view;view.setSource(QUrl(“qrc:/main.qml”);view.show();return app.exec();project/main.qmlimport Qt 4.7Image source: “images/background.png”请注意,资源系统是不能从QML直接访问的。如果主QML文件被加载作为资源,所有的文件指定在QML中做为相对路径从资源系统载入。在QML层使用资源系统是完全透明的。这也意味着,如果主QML文件没有被加载作为资源,那么从QML不能访问资源系统。1.这里主要是介绍,如何在c+中调用QML中的函数和设置QML中的属性的问题2.具体代码/ UICtest.qmlimport Qt 4.7Rectangle id: mainWidget; width: 640 height: 480 function callbyc(v) mainWidget.color = v; return finish; Rectangle id: secondRect; x: 100; y: 20; width: 400; height: 300; Rectangle x: 10; y: 20; width: 30; height: 40; color: #FF035721 Text objectName: NeedFindObj; anchors.fill: parent; text: ; / main.cpp#include #include #include #include #include #include #include int main(int argc, char *argv) QApplication a(argc, argv); QDeclarativeView qmlView; qmlView.setSource(QUrl:fromLocalFile(./UICtest/UICtest.qml); qmlView

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论