Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1067064
  • 博文数量: 264
  • 博客积分: 6005
  • 博客等级: 大校
  • 技术积分: 2798
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-08 20:15
文章分类

全部博文(264)

文章存档

2011年(42)

2010年(213)

2009年(4)

2008年(2)

2007年(3)

分类: C/C++

2011-03-06 21:35:30

main.cpp   =====call====>  main.qml   ==call==> componentCreation.js  ====call===> Sprite.qml

根据官方文档改写。

========================= main.cpp ============================
#include #include "qmlapplicationviewer.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); QmlApplicationViewer viewer; viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto); viewer.setMainQmlFile(QLatin1String("qml/SensorControl/main.qml")); viewer.showExpanded(); return app.exec(); }


========================main.qml==================================
import QtQuick 1.0 import "componentCreation.js" as MyScript Rectangle { id: appWindow width: 400; height: 400 color: "blue" property variant obj;
  Rectangle{ id: window1 width: 20; height: 20 color: "red" MouseArea { id: mouseArea1 anchors.fill: parent onClicked:{ window2.visible = false;/**/} } Component.onCompleted: { mouseArea1.clicked.connect(MyScript.jsFunction) } } Rectangle{ id: window2 width: 120; height: 120 color: "yellow"; x:40;y:40 Rectangle{ id: window3 width: 50; height: 50 color: "green"; } } Rectangle{ id: window4 width: 50; height: 50 color: "green"; x:window2.x+20;
y:window2.y+20; Rectangle{ id: window5 width: 20; height: 20 color: "black" MouseArea { id: mouseArea5 anchors.fill: parent onClicked:{ window2.visible = true; /*;*/ window2.x = window2.x + 10; obj.y = 10;} } } }
  Component.onCompleted:{ obj = MyScript.createSpriteObjects(window2); obj.y = 200;/* newWindow.destroy(1000);*/} }


========================componentCreation.js========================== var component; function createSpriteObjects(parentId) { component = Qt.createComponent("Sprite.qml"); if (component.status == Component.Ready) return finishCreation(parentId); else component.statusChanged.connect(finishCreation); } function finishCreation(parentId) { if (component.status == Component.Ready) { var sprite = component.createObject(parentId/*appWindow*/, {"x": 0, "y": 0}); if (sprite == null) { // Error Handling console.log("Error creating object"); } sprite.y = 200; return sprite; } else if (component.status == Component.Error) { // Error Handling console.log("Error loading component:", component.errorString()); } } function jsFunction() { console.log("Called JavaScript function!") }

========================= Sprite.qml ==================
import QtQuick 1.0 Rectangle { width:50; height: 50; color: "red" MouseArea { id: mouse_area1 anchors.fill: parent onEntered: parent.width +=10; onExited: parent.width -=10; } }



阅读(4302) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~