QML 与 C++(Qt)通过信号和槽进行交互

<ul> <li> <div align="left">清楚Qt的都清楚信号的槽,它无比的强大,怎么强大呢这里就不解释了。<a href="http://www.ibm.com/developerworks/cn/linux/guitoolkit/qt/signal-slot/" target="_blank">信号与槽机制介绍</a></div> </li> <li> <div align="left">当然都是Qt的东西,QML也可以用信号和槽,现在介绍一下他们通过信号和操怎么互传数据</div> </li> <li> <div align="left">继承QDeclarativeView</div> </li> <li> <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:10c9203c-57c5-48c4-a81a-d57ffd630a1e" class="wlWriterEditableSmartContent"><pre class="brush: cpp; gutter: false; first-line: 1; tab-size: 4; toolbar: false; width: 400px; height: 58px;" style=" width: 400px; height: 58px;overflow: auto;">QObject *rootObject = reinterpret_cast&lt;QObject*&gt;(this-&gt;rootObject());</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div> </li>

<li>得到rootObject,返回的是一个QGraphicsObject的值,这里的把他强制转换成QObject才能使用信号和槽。 </li> </ul>

<h2>接收QML信号</h2>

<ul> <h2></h2> </ul>

<blockquote> <p>QML内容</p> </blockquote>

<ul> <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:836eca43-a252-470a-8833-4062530a7893" class="wlWriterEditableSmartContent"><pre class="brush: css; gutter: false; first-line: 1; tab-size: 4; toolbar: false; width: 400px; height: 288px;" style=" width: 400px; height: 288px;overflow: auto;">import QtQuick 2.0

Rectangle { id:roots width: 100 height: 62 color:"#cccccc" signal singalText1(string valueStr); Text{ id:text1 text:"hello word 2" } MouseArea{ onClicked:roots.singalText1(text1.text); } }</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>

</ul>

<ul></ul>

<ul></ul>

<p></p>

<blockquote> <p>接收Qml的信号</p> </blockquote>

<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:efc5367f-113e-46a7-83f0-4bb2e8073f74" class="wlWriterEditableSmartContent"><pre class="brush: cpp; gutter: false; first-line: 1; tab-size: 4; toolbar: false; width: 400px; height: 70px;" style=" width: 400px; height: 70px;overflow: auto;">connect(rootObject, SIGNAL(singalText1(QString )),this,SLOT(slotsQML(QString )));</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>

<h2>向QML传值</h2>

<blockquote> <p>QML内容</p> </blockquote>

<p></p>

<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:4e05bf39-8a81-45ae-9e46-d98472b0f7d7" class="wlWriterEditableSmartContent"><pre class="brush: xml; gutter: false; first-line: 1; tab-size: 4; toolbar: false; width: 400px; height: 108px;" style=" width: 400px; height: 108px;overflow: auto;">import QtQuick 2.0

Rectangle { id:roots width: 100 height: 62 color:"#cccccc" signal singalText1(string valueStr); function slotsStr(value){ text1.text = value }

Text{
    id:text1
    text:&quot;hello word 2&quot;
}
MouseArea{
    onClicked:roots.singalText1(text1.text);
}

}</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>

<p></p>

<blockquote> <p>Qt</p> </blockquote>

<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:512f7353-b9ec-4067-b9fc-bdcba084baf9" class="wlWriterEditableSmartContent"><pre class="brush: cpp; gutter: false; first-line: 1; tab-size: 4; toolbar: false; width: 400px; height: 180px;" style=" width: 400px; height: 180px;overflow: auto;">void setText(QString value){ emit singalSetText(value) } connect(this,SIGNAL(singalSetText(QVariant)),rootObject,SLOT(boatMark(QVariant)));</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>

转载于:https://my.oschina.net/bighome/blog/262226

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值