• 大小: 14KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-18
  • 语言: 其他
  • 标签: Qt  键盘事件  

资源简介

qt 捕获键盘事件,控制对象的移动。keyPressEvent()和keyReleaseEvent()

资源截图

代码片段和文件信息

#include “keyevent.h“
#include 
KeyEvent::KeyEvent(QWidget *parent)
    : QWidget(parent)
{
    setWindowtitle(tr(“键盘事件“));
    setAutoFillBackground(true);
    QPalette palette = this->palette();
    palette.setColor(QPalette::WindowQt::white);
    setPalette(palette);
    setMinimumSize(512256);
    setMaximumSize(512256);
    width=size().width();
    height=size().height();
    pix = new QPixmap(widthheight);
    pix->fill(Qt::white);
    image.load(“../image/image.png“);
    startX=100;
    startY=100;
    step=20;
    drawPix();
    resize(512256);
}

void KeyEvent::drawPix()
{
    pix->fill(Qt::white); //重新刷新pix对象为白色底色
    QPainter *painter = new QPainter; //创建一个QPainter对象
    QPen pen(Qt::DotLine); //(a)
    for(int i=step;i    {
        painter->begin(pix); //指定pix为绘图设备
        painter->setPen(pen);
        painter->drawLine(QPoint(i0)QPoint(iheight));
        painter->end();
    }
    for(int j=step;j    {
        painter->begin(pix);
        painter->setPen(pen);
        painter->drawLine(QPoint(0j)QPoint(widthj));
        painter->end();
    }
    painter->begin(pix);
    painter->drawImage(QPoint(startXstartY)image); //(b)
    painter->end();
}

void KeyEvent::keyPressEvent(QKeyEvent *event)
{
    if(event->modifiers()==Qt::ControlModifier) //(a)
    {
        if(event->key()==Qt::Key_Left) //(b)
        {
            startX=(startX-1<0)?startX:startX-1;
        }
        if(event->key()==Qt::Key_Right) //(c)
        {
            startX=(startX+1+image.width()>width)?startX:startX+1;
        }
        if(event->key()==Qt::Key_Up) //(d)
        {
            startY=(startY-1<0)?startY:startY-1;
        }
        if(event->key()==Qt::Key_Down) //(e)
        {
            startY=(startY+1+image.height()>height)?startY:startY+1;
        }
    }
    else //对【Ctrl】键没有按下的处理
    {
        /* 首先调节图标左上顶点的位置至网格的顶点上 */
        startX=startX-startX%step;
        startY=startY-startY%step;
        if(event->key()==Qt::Key_Left) //(f)
        {
            startX=(startX-step<0)?startX:startX-step;
        }
        if(event->key()==Qt::Key_Right) //(g)
        {
            startX=(startX+step+image.width()>width)?startX:startX+step;
        }
        if(event->key()==Qt::Key_Up) //(h)
        {
            startY=(startY-step<0)?startY:startY-step;
        }
        if(event->key()==Qt::Key_Down) //(i)
        {
            startY=(startY+step+image.height()>height)?
            startY:startY+step;
        }
        if(event->key()==Qt::Key_Home) //(j)
        {
            startX=0;
            startY=0;
        }
        if(event->key()==Qt::Key_End) //(k)
        {
            startX=width-image.width();
            startY=height-image.height();
        }
    }
    drawPix(); //根据调整后的图标位置重

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3675  2015-03-26 13:51  image\image.png

    ..A..H.      4608  2015-03-26 13:51  image\Thumbs.db

     文件       3675  2015-03-26 13:51  KeyEvent\image\image.png

    ..A..H.      4608  2015-03-26 13:51  KeyEvent\image\Thumbs.db

     文件       3465  2017-02-18 15:47  KeyEvent\keyevent.cpp

     文件        697  2017-02-18 15:47  KeyEvent\keyevent.h

     文件       1026  2017-02-18 14:47  KeyEvent\KeyEvent.pro

     文件        110  2017-02-18 15:47  KeyEvent\keyevent.qrc

     文件        179  2017-02-18 14:28  KeyEvent\main.cpp

     目录          0  2017-04-21 13:47  KeyEvent\image

     目录          0  2017-04-21 13:47  image

     目录          0  2019-10-28 20:42  KeyEvent

----------- ---------  ---------- -----  ----

                22043                    12


评论

共有 条评论