您的位置:首页 > 编程语言 > Qt开发

自学QT之QML实现响应鼠标和键盘事件

2015-11-22 19:45 1581 查看
懒得写那么多,好吧,太懒了,把解释都写在了代码的注释了,一看就明白的。很简单。

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";
    }
    }
    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: