QML 基本问题 关于 Timer的

tt2com 2012-07-04 02:13:49
我想坐一个按键长时间响应的动作,在c++中我用QTimer 来实现,但我需要获取QTimer 的运行时间,但好像QML 中的Timer好像无法获取已经运行的时间。

如何实现 onPressAndHold 的事件。
onPressAndHold 固定800ms,我希望能够实现时间可调整的,该如何处理
...全文
823 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tt2com 2012-07-04
  • 打赏
  • 举报
回复
哎 放了个2b错误 吧true 写成了 ture
tt2com 2012-07-04
  • 打赏
  • 举报
回复

import QtQuick 1.0

Item {
id:zone_detail
width: 98
height: 98
property int m_state: 0
property string content1: ""
property string content2: ""
property string content3: ""
property bool m_dd: false;
function set_state(state_){
switch(state_){
case 0:
zone_detail.state = "";
break;
case 1:
zone_detail.state = "state1";
break;
case 2:
zone_detail.state = "state2";
break;
}
return state_;
}
Timer{
id: timer1
interval: 300;
onTriggered: {
zone_detail.m_dd = ture;
}
}
BorderImage {
id: border_image1
anchors.fill: parent
source: ..

Text {
id: text1
x: 0
y: 23
width: 98
height: 21
text: content1
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
font.pixelSize: 18
}

Text {
id: text2
x: 0
y: 66
width: 49
color: "#ffffff"
text: content3
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
font.pixelSize: 12
}

Text {
id: text3
x: 49
y: 66
width: 49
color: "#ffffff"
text: content2
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
font.pixelSize: 12
}
}

MouseArea {
id: mouse_area1
anchors.fill: parent
onPressed: timer1.start();
onReleased: {
timer1.stop();
if(!m_dd){
m_state=set_state(m_state);
}
else{
m_state = set_state(0);
m_dd = false;
}
}
onPositionChanged:set_state(m_state);
}
states: [
State {
name: "state1";when: mouse_area1.pressed && m_state==0

PropertyChanges {
target: border_image1
source: "../../qml_vrf/qml_vrf/Resources/image/group_all/group_back_select.png"
}
},
State {
name: "state2";

PropertyChanges {
target: border_image1
source: ..
}

PropertyChanges {
target: text1
visible: false
}

PropertyChanges {
target: text2
visible: false
}

PropertyChanges {
target: text3
visible: false
}
},
State {
name: "state3";when: mouse_area1.pressed && m_state==1
PropertyChanges {
target: border_image1
source: ..
}

PropertyChanges {
target: text1
visible: false
}

PropertyChanges {
target: text2
visible: false
}

PropertyChanges {
target: text3
visible: false
}
},
State {
name: "state4";
PropertyChanges {
target: border_image1
source: ..
}

PropertyChanges {
target: text1
visible: false
}

PropertyChanges {
target: text2
visible: false
}

PropertyChanges {
target: text3
visible: false
}
},
State {
name: "state5";when: mouse_area1.pressed && m_state==2
PropertyChanges {
target: border_image1
source: ..
}

PropertyChanges {
target: text1
visible: false
}

PropertyChanges {
target: text2
visible: false
}

PropertyChanges {
target: text3
visible: false
}
}
]
}

tt2com 2012-07-04
  • 打赏
  • 举报
回复
我郁闷了 为什么我的一值没法起来
Vegata 2012-07-04
  • 打赏
  • 举报
回复
import QtQuick 1.1

Rectangle {
width: 360
height: 360

property bool isLongHold:false
Rectangle
{
anchors.centerIn: parent
width: 60
height: 60
color: "lightblue"
Timer
{
id:timer
interval: 3000
onTriggered: isLongHold = true
}

MouseArea {
anchors.fill: parent
onPressed: timer.start()
onReleased:
{
timer.stop()
if(isLongHold)
{
console.log("long hold")
}
else
{
console.log("not long hold")
}
isLongHold = false
}
}
}
}


我试验了下 没问题 可以实现
tt2com 2012-07-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

我的想法是

1 首先设置一个 bool类型的属性 isLongHold
2 onPressed事件的时候Timer开始计时 当Timer triggered的时候将isLongHold设为true
3 onRelease事件时根据 isLongHold 的值判断是否进行长按操作

没找到onPressAndHold 改时间的方法....
[/Quote]
我现在就是这么做的,但事实是不行
Vegata 2012-07-04
  • 打赏
  • 举报
回复
我的想法是

1 首先设置一个 bool类型的属性 isLongHold
2 onPressed事件的时候Timer开始计时 当Timer triggered的时候将isLongHold设为true
3 onRelease事件时根据 isLongHold 的值判断是否进行长按操作

没找到onPressAndHold 改时间的方法....

16,248

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧