qml學習-----------按鍵訊息處理

來源:互聯網
上載者:User

本次學習了如何處理qml中的按鍵訊息,其實也很簡單,主要就是有個訊息傳遞的問題需要處理。
如果不向下傳遞訊息,後面則無法接收到按鍵訊息了。
下面就是具體的例子:

import QtQuick 2.2import QtQuick.Controls 1.2Rectangle{    //視窗的寬度    width: 320;    //視窗的高度    height: 480;    //視窗的顏色    color:"gray";    focus: true;    //是否處理按鍵    Keys.enabled: true;    //處理按鍵的ESC訊息    Keys.onEscapePressed: {        Qt.quit();    }    //訊息傳遞到   moveText,likeQt處理。 其中需要注意的是,moveText處理的按鍵訊息,likeQt就收不到了    Keys.forwardTo: [moveText , likeQt];    Text{        id:moveText;        x:20 ;        y:20;        width: 200;        height: 30;        text:"Moving Text";        color:"blue";        //設定字型,大小        font {  bold :true; pixelSize :24;}        //是否處理按鍵        Keys.enabled: true;        //處理按鍵訊息        Keys.onPressed: {            switch(event.key){            case Qt.Key_Left:                x -= 10;                break;            case Qt.Key_Right:                x+= 10;                break;            case Qt.Key_Down:                y += 10;                break;            case Qt.Key_Up:                y -= 10;                break;            default:                return;            }            //繼續傳遞按鍵,讓後面可以處理按鍵訊息            event.accepted = true;        }    }    CheckBox{        //為啥按下空格就會自動選中或者取消呢。 是因為qt quick中已經實現了處理按鍵        id:likeQt;        text:"Like Qt Quick";        anchors.left: parent.left;        anchors.leftMargin: 10;        anchors.bottom: parent.bottom;        anchors.bottomMargin: 10;        z:1;    }}

注釋都寫好啦,就不做多的解釋啦~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.