/** * 书本:【Qt5开发及实例】 * 功能:实现键盘控制 * 文件:keyevent.h * 时间:2015年2月8日11:48:47 * 作者:cutter_point */ #ifndef KEYEVENT_H #define KEYEVENT_H #include <QWidget> #include <QKeyEvent> //键盘 #include <QPaintEvent> //绘画事件 #include <QPainter> class KeyEvent : public QWidget { Q_OBJECT public: KeyEvent(QWidget *parent = 0); ~KeyEvent(); void drawPix(); //绘画图像 void keyPressEvent(QKeyEvent *event); //键盘相应 void paintEvent(QPaintEvent *); private: QPixmap *pix; //缓冲图像 QImage image; //图片 int startX; int startY; int width; int height; int step; //每步的大小 }; #endif // KEYEVENT_H
/** * 书本:【Qt5开发及实例】 * 功能:实现键盘控制 * 文件:keyevent.cpp * 时间:2015年2月8日11:48:47 * 作者:cutter_point */ #include "keyevent.h" KeyEvent::KeyEvent(QWidget *parent) : QWidget(parent) { setWindowTitle(tr("键盘事件")); this->setAutoFillBackground(true); QPalette palette = this->palette(); //取得调色板 palette.setColor(QPalette::Window, Qt::white); //设置背景默认颜色 this->setPalette(palette); //设置好调色板,使用调色板 this->setMinimumSize(512, 256); this->setMaximumSize(512, 256); width = this->size().width(); height = this->size().height(); //得到相应的长和高 pix = new QPixmap(width, height); //缓冲图形的大小 pix->fill(Qt::white); image.load(":/image.png"); //加载图片 startX = 100; startY = 100; step = 20; drawPix(); //绘图 resize(512, 256); } //void drawPix(); //绘画图像 void KeyEvent::drawPix() { pix->fill(Qt::white); QPainter *painter = new QPainter; //绘画工具 QPen pen(Qt::DotLine); //虚线 for(int i = step; i < width; i = i + step) //画竖线 { painter->begin(pix); //设定绘画对象 painter->setPen(pen); //画笔 painter->drawLine(QPoint(i, 0), QPoint(i, height)); //从一个点画到另外一个点 painter->end(); } //画横线 for(int j = step; j < height; j = j + step) { painter->begin(pix); painter->setPen(pen); painter->drawLine(QPoint(0, j), QPoint(width, j)); painter->end(); } painter->begin(pix); painter->drawImage(QPoint(startX, startY), image); //图片绘制 painter->end(); } //keyPressEvent(QKeyEvent *event); //键盘相应 void KeyEvent::keyPressEvent(QKeyEvent *event) { if(event->modifiers() == Qt::ControlModifier) //是否有按下Ctrl { if(event->key() == Qt::Key_Left) //左 { startX = (startX - 1 < 0) ? startX : startX - 1; } if(event->key() == Qt::Key_Right) //右 { startX = (startX + image.width() > width) ? startX : startX + 1; } if(event->key() == Qt::Key_Up) //上 { startY = (startY - 1 < 0) ? startY : startY - 1; } if(event->key() == Qt::Key_Down) //下 { startY = (startY + image.height() > height) ? startY : startY + 1; } } else { startX = startX - startX % step; //化为step的倍数,后面好判定 startY = startY - startY % step; if(event->key() == Qt::Key_Left) //左 { startX = (startX - step < 0) ? startX : startX - step; } if(event->key() == Qt::Key_Right) //右 { startX = (startX + step + image.width() > width) ? startX : startX + step; } if(event->key() == Qt::Key_Up) //上 { startY = (startY - step < 0) ? startY : startY - step; } if(event->key() == Qt::Key_Down) //下 { startY = (startY + image.height() + step > height) ? startY : startY + step; } if(event->key() == Qt::Key_Home) //起点 { startX = 0; startY = 0; } if(event->key() == Qt::Key_End) //终点 { startX = width - image.width(); startY = height - image.height(); } } drawPix(); this->update(); } //void paintEvent(QPaintEvent *); void KeyEvent::paintEvent(QPaintEvent *) { QPainter painter; painter.begin(this); painter.drawPixmap(QPoint(0, 0), *pix); //把图像画到空间,显示 painter.end(); } KeyEvent::~KeyEvent() { }
原文地址:http://blog.csdn.net/cutter_point/article/details/44263739