QT QML Keys 处理注意事项

【生活经历分享】华师国培 华师伴学 合同都是坑 消费者付款后无法退款
和华师国培签合同需小心,合同中都是保护华师的条款,没有保护消费者的条款。
收到钱,就算你因对培训质量不满意,也不能退款。因合同消费者维权肯定十分艰难。
华师伴学的授课方式是看录制的视频,不是真人现场教学。是否是您和孩子想要的学习方式?
各位打算报名的,交费要谨慎!

其他人在小红书上发的,转:

深圳市华师国培教育科技有限公司,黑心机构,大家擦亮眼睛,别被骗了,消费欺诈,虚假承诺,签合同各种坑,收到钱了不履行承诺不退款,乱扣费,维权艰难! - 小红书

 

今天在学习 QT QML 最基本的东东,在下面的代码中响应按键处理无效。代码如下:

 1 import QtQuick 2.2
 2 import QtQuick.Window 2.1
 3 import QtQuick.Controls 1.2
 4 
 5 Window {
 6     visible: true
 7     width: 360
 8     height: 360
 9 
10     MouseArea {
11         anchors.fill: parent
12         onClicked: {
13             Qt.quit();
14         }
15     }
16 
17     Text {
18         id: helloWorldTxt;
19         text: qsTr("Hello World")
20         anchors.centerIn: parent
21     }
22 
23     Button {
24         id: quitBtn;
25         text: "Quit";
26         anchors.horizontalCenter: helloWorldTxt.horizontalCenter;
27         anchors.top: helloWorldTxt.bottom;
28         anchors.bottomMargin: 8;
29         onClicked: {
30             console.log("click quit button.");
31             Qt.quit();      // 如果没有此行,则点击此按键不会退出.
32         }
33     }
34 
35     // focus: true;            // 错误: Invalid property name
36     Keys.enabled: true;
37     Keys.onEscapePressed: {
38         Qt.quit();  // 没有功能: 不退出, why?
39     }
40     Keys.onPressed: {
41         switch(event.key) {
42         case Qt.Key_0:
43             console.log("click key 0.");        // 一样没有 LOG 输出
44             break;
45         default:
46             console.log("click key other.");
47             break;
48         }
49     }
50 }

将 Keys 的处理放在 Item 中、并且增加: focus: true; 后,功能正常。

代码如下(省略部分重复的代码):

 1 import QtQuick 2.2
 2 import QtQuick.Window 2.1
 3 import QtQuick.Controls 1.2
 4 
 5 Window {
 6     ......
 7 
 8     Item {
 9         focus: true;            // 此句是必须的, 否则没有功能
10         Keys.enabled: true;
11         Keys.onEscapePressed: {
12             Qt.quit();  // 功能正常
13         }
14         Keys.onPressed: {
15             switch(event.key) {
16             case Qt.Key_0:
17                 console.log("click key 0.");        // 有 LOG 输出
18                 break;
19             default:
20                 console.log("click key other.");
21                 break;
22             }
23         }
24     }
25 }

 

posted @ 2016-10-12 12:29  91program  阅读(2295)  评论(0)    收藏  举报