qml鼠标键盘事件

鼠标事件、拖拽、键盘事件、定时器、动态加载组件。设置了anchors属性就不能拖拽了,鼠标按下改变颜色、使用KeyNavigation的left、up、right、down切换选择对象,定时器更新时间等。
main.cpp

#include <QGuiApplication>
#include <QQuickView>
int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQuickView viewer;
    viewer.setResizeMode(QQuickView::SizeRootObjectToView);
    viewer.setSource(QUrl("qrc:/main.qml"));
    viewer.show();
    return app.exec();
}

main.qml

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
Rectangle
{
    width:640
    height:480
    color: "lightgray"
    Rectangle
    {
        id:rect
        width:parent.width/3
        height: parent.height/3
        //不能拖拽设置了anchors的控件
        //anchors.horizontalCenter: parent.horizontalCenter
        //anchors.verticalCenter: parent.verticalCenter
        color: "red"
        focus: true
        MouseArea
        {
            anchors.fill: parent
            //允许、按下、包含鼠标、报告鼠标位置等改变的消息
            enabled: true
            //read only property
            //pressed
            //containsMouse:
            hoverEnabled: true
            //接收的鼠标事件,默认似乎是鼠标左键
            acceptedButtons: Qt.LeftButton | Qt.MidButton | Qt.RightButton
            //鼠标事件改变颜色
            onClicked:
            {
                if(mouse.button == Qt.LeftButton)
                    parent.color = "green"
                else if(mouse.button == Qt.RightButton)
                    parent.color = "blue"
                //control + 鼠标中键
                else if(mouse.modifiers == Qt.ControlModifier && mouse.button == Qt.MidButton)
                    parent.color = "red"
            }
            //拖拽 不能拖拽设置了anchors的控件
            drag
            {
                target:rect
                axis:Drag.XAndYAxis
            }
        }
        //键盘事件
        Keys.onPressed:
        {
            //Ctrl + A
            if(event.modifiers == Qt.ControlModifier && event.key == Qt.Key_A)
            {
                console.log("Ctrl + A pressed.")
                //接受事件,不再继续传递
                event.accepted = true
            }
        }
    }
    //按键处理,KeyNavigation导航界面,2*2棋盘格子切换
    Grid
    {
        anchors.centerIn: parent
        columns: 2
        spacing: 5
        Block
        {
            id:topLeft
            text:"1"
            KeyNavigation.right: topRight
            KeyNavigation.down: bottomLeft
        }
        Block
        {
            id:topRight
            text:"2"
            KeyNavigation.left: topLeft
            KeyNavigation.down: bottomRight
        }
        Block
        {
            id:bottomLeft
            text:"3"
            KeyNavigation.right: bottomRight
            KeyNavigation.up:topLeft
        }
        Block
        {
            id:bottomRight
            text:"4"
            KeyNavigation.left: bottomLeft
            KeyNavigation.up: topRight
        }
    }
    Column
    {
        anchors.horizontalCenter: parent.horizontalCenter
        //用activeFocus判断是否已经有焦点
        Text
        {
            text:"I" + (activeFocus ? "" : " do not") + " have focus."
            MouseArea
            {
                anchors.fill: parent
                onClicked: parent.focus = true
            }
        }
        //定时器
        Text
        {
            Timer
            {
                //时间、运行、重复、槽
                interval: 200
                running: true
                repeat: true
                onTriggered: parent.text = Date().toString()
            }
            font{family: "微软雅黑";pointSize: 15}
        }
        Item
        {
            //Loader动态加载控件
            width:100
            height:100
            Loader{id:loader}
            MouseArea
            {
                anchors.fill: parent
                onClicked: loader.source="Block.qml"
            }
        }
    }
}

Block.qml

import QtQuick 2.0
Rectangle
{
    property alias text: blockText.text
    width:80;height:80
    color:focus ? "red" : "gray"
    //鼠标按下设置焦点
    MouseArea
    {
        anchors.fill: parent
        onClicked: {parent.focus = true}
    }
    Text
    {
        id:blockText
        anchors.centerIn: parent
        font.family: "微软雅黑"
        font.pixelSize: 30
    }
}

效果:
这里写图片描述
代码:https://github.com/yangyang0312/cpp/tree/master/Qt/qml/qmlEvent

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值