自學QT之QML實現響應鼠標和鍵盤事件

懶得寫那麼多,好吧,太懶了,把解釋都寫在了代碼的註釋了,一看就明白的。很簡單。佈局

import QtQuick 2.3
import QtQuick.Window 2.2
import QtQuick.Controls 1.2

Window {
    visible: true

    Text {
        id: helloworld;
        //anchors.centerIn: parent;//要想實現控件的移動,就必須把這個佈局給屏幕,不然移動是無效的
        text: qsTr("helloworld");
        font{
        bold: false;
        pixelSize: 20;
        }
        Keys.onLeftPressed: x-=10;
        Keys.onRightPressed: x+=10;
        focus: true;//只有得到焦點的控件纔可以響應鼠標事件
    }
    MouseArea{
    anchors.fill: parent;//鼠標響應區域填充爲整個區域
    acceptedButtons: Qt.LeftButton|Qt.RightButton;
    //響應單擊事件
    onClicked: {
        if(mouse.button==Qt.LeftButton)
        {
        helloworld.text="eeeeee";

        }
        else
        {
        helloworld.text="wwww";
        }
    }
    //響應雙擊事件
    onDoubleClicked: {
    if(mouse.button==Qt.LeftButton){
    helloworld.text="left double";
    }
    else

    {
        helloworld.text="right double";
    }
    }
    }


}
相關文章
相關標籤/搜索