qt4.7 qml互相调用

         今天去参加了nokia的code print,还是学到些东西的,nokia用qml脚本实现前端的ui酷炫效果,后边用js或者实现逻辑,看起来很强大,产品设计者直接用ui designer设计ui,programmer直接在ui上开发逻辑,省去了symbian前端ui大量的繁琐工作,也发现些缺点,就是qml不能断点跟踪,只能打log。
         在此qml调用js的就不介绍了,调用c++的部分还是蛮不错的,再复杂的程序,用c++实现,也是蛮不错;一年前有个code print活动据说搞出来连连看的demo很炫,这次活动上就花几个小时,刘晓国老师、啸天大神也弄出来不错的东东了,还有好几个哥们做的游戏也是很炫的,有查找2张图片中不同的3个地方,有个飞转的demo。


         此例子是可以运行的,demo上的qml文件有些问题。

 

         qml能否成为nokia的杀手锏,还要我们拭目以待。

To expose data to a QML component instance, applications set context properties which are then accessible by name from QML Property Bindings and JavaScript. The following example shows how to expose a background color to a QML file through QDeclarativeView:

// main.cpp
 #include <QApplication>
 #include <QDeclarativeView>
 #include <QDeclarativeContext>
// main.cpp
 class Stopwatch : public QObject
 {
     Q_OBJECT
 public:
     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"));//qt调用qml
    //view.setSource(QUrl("qrc:/main.qml"));
    view.show();

    return app.exec();
 }

// main.qml
import Qt 4.7

Rectangle {
    width: 300
    height: 300
    radius: 20

    Text {
        id: helloText
        text: "ImageView"
    x: 80
        y: 5
        anchors.horizontalCenter: page.horizontalCenter
        font.pointSize: 24; font.bold: true
    }
 
    MouseArea {
        id: mouseArea; anchors.fill: parent
        onClicked: {
            if (stopwatch.isRunning())//qml调用c++方法
            {
                stopwatch.stop()  ;
                helloText.color = "black";
            }
            else
            {
                 stopwatch.start();//qml调用c++方法
                 helloText.color = "red";
            }
        }
    }
}

 

转自:http://wiki.forum.nokia.com/index.php/Qt_and_qml_Call_each_other

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值