找回密码
 立即注册
收起左侧

在C++中调用QML文件出错

2
回复
9474
查看
[复制链接]

尚未签到

来源: 2015-4-17 11:58:04 显示全部楼层 |阅读模式
1Qter豆
  1. //main.cpp
  2. #include "test.h"
  3. #include <QApplication>

  4. int main(int argc, char *argv[])
  5. {
  6.     QGuiApplication a(argc, argv);
  7. //    test  t1;
  8.     QQuickView view;
  9. //    view.setFlags(Qt::FramelessWindowHint);
  10.     view.setSource(QUrl(QStringLiteral("qrc:/qml/simple_button.qml")));

  11. //    view.rootContext()->setContextProperty("simple_button",&view);
  12.     view.show();
  13.     Sleep(50000);
  14.     return a.exec();
  15. }

  16. //simple_button.qml
  17. import QtQuick 2.2

  18. Item
  19. {
  20.     id:ite
  21.     width: 400
  22.     height: 400

  23.     Rectangle    //背景颜色
  24.     {
  25.         id:background
  26.         anchors.fill: parent
  27.         color: "black"

  28.         Rectangle  //按钮
  29.         {
  30.             id:button
  31.             width: 250
  32.             height: 50
  33.             color:"black"
  34.             border.color: "white"
  35.             border.width: 4
  36.             state:"RELEASED"
  37.             anchors.horizontalCenter: parent.horizontalCenter
  38.             anchors.verticalCenter: parent.verticalCenter

  39.             signal qmlsignal(string msg)
  40.             onQmlsignal:
  41.             {
  42.                 console.log(msg)

  43.             }

  44.             Text
  45.             {
  46.                 id: texx
  47.                 text: qsTr("开始")
  48.                 font.family: "微软雅黑"
  49.                 font.pointSize: 16
  50.                 color: "white"
  51.                 anchors.horizontalCenter: parent.horizontalCenter
  52.                 anchors.verticalCenter: parent.verticalCenter
  53.             }

  54.                 MouseArea
  55.                 {
  56.                     anchors.fill: parent
  57.                     onPressed:button.state="PRESSED"
  58.                     onReleased:button.state="RELEASED"
  59.                     onClicked: button.qmlsignal("hello world")
  60.                 }

  61.                 states:
  62.                 [
  63.                     State
  64.                     {
  65.                         name:"PRESSED"
  66.                         PropertyChanges
  67.                         {
  68.                             target: button
  69.                             color:"white"
  70.                         }
  71.                         PropertyChanges
  72.                         {
  73.                             target: texx
  74.                             color:"black"
  75.                         }
  76.                         PropertyChanges
  77.                         {
  78.                             target: texx
  79.                             font.pointSize: 18
  80.                         }
  81.                     },

  82.                     State
  83.                     {
  84.                         name:"RELEASED"
  85.                         PropertyChanges
  86.                         {
  87.                             target: button
  88.                             color: "black"
  89.                         }
  90.                         PropertyChanges
  91.                         {
  92.                             target: texx
  93.                             font.pointSize: 16
  94.                         }
  95.                         PropertyChanges
  96.                         {
  97.                             target: texx
  98.                             color:"white"
  99.                         }
  100.                     }

  101.                 ]

  102.                 transitions:
  103.                 [
  104.                     Transition
  105.                     {
  106.                         from: "PRESSED"
  107.                         to: "RELEASED"
  108.                         ColorAnimation {target: button; from: "white"; to: "black"; duration: 500 }
  109.                     },

  110.                     Transition
  111.                     {
  112.                         from:"RELEASED"
  113.                         to:"PRESSED"
  114.                         ColorAnimation {target: button;duration: 100 }
  115.                     }
  116.                 ]
  117.         }
  118.     }
  119. //现在的问题是调用成功以后出现画面但是是未响应的画面、、 求教各位大神。头回调用QML文件
复制代码

最佳答案

查看完整内容

不懂你后面加个sleep(50000)是干嘛。。。这样整个程序不是会暂停50秒么? qml的话你可以用qmlscene这个命令单独测试某个qml文件
回复

使用道具 举报

尚未签到

2015-4-17 11:58:05 显示全部楼层
不懂你后面加个sleep(50000)是干嘛。。。这样整个程序不是会暂停50秒么?


qml的话你可以用qmlscene这个命令单独测试某个qml文件
回复

使用道具 举报

尚未签到

2015-4-17 11:59:55 显示全部楼层
那里面叫做test的类不用管他,因为我本来想把QML文件封装到这个类里面的,这个类里面暂时还没有写什么东西。(其实本想把这一段在main里写的代码写在test类的构造函数里面的)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了