Qt键盘事件

  • 新建桌面应用程序testKeyEvent,类名KeyEvent,基类QWidget,通过重写键盘事件到达操作的键盘的时候窗口中的图片上下左右移动,类似QtCreator设计模式下通过按键摆放部件时的效果。
  • 头文件keyevent.h添加按键事件、绘图事件及drawpix()的声明,并添加成员数据。

keyevent.h

#ifndef KEYEVENT_H
#define KEYEVENT_H
#include 

namespace Ui { class KeyEvent; }

class KeyEvent : public QWidget {
    Q_OBJECT
public:
    explicit KeyEvent(QWidget *parent = 0);
    ~KeyEvent();
    void drawPix();                         //绘制图片

protected:
    void keyPressEvent(QKeyEvent *event);   //键盘事件
    void paintEvent(QPaintEvent *event);    //绘图事件

private:
    Ui::KeyEvent *ui;
    QPixmap *m_pix;     //位图
    QImage m_image;     //图片
    int m_startX;       //图片x坐标
    int m_startY;       //图片y坐标
    int m_width;        //宽度
    int m_height;       //高度
    int m_step;         //步长
};
#endif // KEYEVENT_H

 
keyevent.cpp

#include "keyevent.h"
#include "ui_keyevent.h"
#include 
#include 
#include 

#define WIDTH   520
#define HEIGHT  280

KeyEvent::KeyEvent(QWidget *parent) : QWidget(parent), ui(new Ui::KeyEvent){
    ui->setupUi(this);
    setWindowTitle("键盘事件");
    setWindowFlags(Qt::WindowCloseButtonHint);
    setFixedSize(WIDTH, HEIGHT);

    m_width = this->size().width();
    m_height = this->size().height();

    m_pix = new QPixmap(m_width,m_height);
    m_pix->fill(Qt::white);

    m_image.load("xiaoxin.png");

    m_startX = 100;
    m_startY = 100;
    m_step = 20;		//步长设置20像素

    drawPix();
}

KeyEvent::~KeyEvent(){
    delete ui;
}

void KeyEvent::drawPix(){
    m_pix->fill(Qt::white);		//图片填充白色

    QPainter painter(this);
    QPen pen(Qt::DotLine);      //虚线画笔
    painter.begin(m_pix);       //指定m_pix为绘图设备
    painter.setPen(pen);        //设置虚线笔

    //按照步长画竖向网格线
    for(int i = m_step; i < m_width; i += m_step){
        painter.drawLine(QPoint(i,0), QPoint(i,m_height));  //drawLine(起始点,终止点)
    }
    //按照步长画横向网格线
    for(int j = m_step; j < m_height; j += m_step){
        painter.drawLine(QPoint(0,j), QPoint(m_width, j));
    }
    painter.end();

    painter.begin(m_pix);
    painter.drawImage(QPoint(m_startX,m_startY), m_image);
    painter.end();  //结束绘图
}

void KeyEvent::keyPressEvent(QKeyEvent *event){
    //如果按下的是Ctrl键,每次移动步长为1像素
    if(event->modifiers() == Qt::ControlModifier){
        if(event->key() == Qt::Key_Left)   //按下的是左键
            m_startX = (m_startX-1) < 0 ? m_startX : m_startX-1;
        if(event->key() == Qt::Key_Right)  //按下的是右键
            m_startX = (m_startX+1+m_image.width()) > m_width ? m_startX : m_startX+1;
        if(event->key() == Qt::Key_Up)     //按下的是上键
            m_startY = (m_startY-1) < 0 ? m_startY : m_startY-1;
        if(event->key() == Qt::Key_Down)   //按下的是下键
            m_startY = (m_startY+1+m_image.height()) > m_height ? m_startY : m_startY+1;
    }else{
        //没有按下Ctrl键,每次移动步长20像素,调整图标左上角位置到网格顶点
        m_startX = m_startX - m_startX % m_step;
        m_startY = m_startY - m_startY % m_step;

        if(event->key() == Qt::Key_Left)   //按下的是左键
            m_startX = (m_startX-m_step) < 0 ? m_startX : m_startX-m_step;
        if(event->key() == Qt::Key_Right)  //按下的是右键
            m_startX = (m_startX+m_step+m_image.width()) > m_width ? m_startX : m_startX+m_step;
        if(event->key() == Qt::Key_Up)     //按下的是上键
            m_startY = (m_startY-m_step) < 0 ? m_startY : m_startY-m_step;
        if(event->key() == Qt::Key_Down)   //按下的是下键
            m_startY = (m_startY+m_step+m_image.height()) > m_height ? m_startY : m_startY+m_step;
    }

    drawPix();      //根据调整后的图标位置重新绘制图像
    this->update(); //重新绘制窗口
}

void KeyEvent::paintEvent(QPaintEvent *event){
    QPainter painter;
    painter.begin(this);
    painter.drawPixmap(QPoint(0,0), *m_pix);
    painter.end();
}

运行效果

你可能感兴趣的:(Qt,qt,ui,开发语言)