• 大小: 47KB
    文件类型: .gz
    金币: 1
    下载: 0 次
    发布日期: 2021-06-05
  • 语言: 其他
  • 标签: 键盘事件  移动  

资源简介

参考的是书上的代码,主要是觉得有趣,修改了一些不足,并加入了随意拉大窗口的部分代码。

资源截图

代码片段和文件信息

#include “keyevent.h“

KeyEvent::KeyEvent(QWidget *parent)
:QWidget(parent)
{
    setWindowtitle(tr(“Key Event“));
    
    setAutoFillBackground(true);
    QPalette palette = this->palette();
    palette.setColor(QPalette::WindowQt::white);
    setPalette(palette);
    
    image.load(“:/images/1.png“);
    QMatrix martix;
    martix.scale(0.50.5);
    image = image.transformed(martix);

    setMinimumSize(image.width()image.height());
    //setMaximumSize(512256);
    resize(512 256);

    step=32;
    width=size().width();
    height=size().height();
    startX=0;
    startY=0;

    pix = new QPixmap(widthheight);
    drawPix();
}

KeyEvent::~KeyEvent()
{
}
             
void KeyEvent::drawPix()
{
    pix->fill(Qt::white);
    QPainter *painter = new QPainter(pix);
    QPen pen(Qt::DotLine);
    painter->setPen(pen);
    for(int i=step;i    {
        painter->drawLine(QPoint(i0)QPoint(iheight));
        i=i+step;
    }
    for(int j=step;j    {
        painter->drawLine(QPoint(0j)QPoint(widthj));
        j=j+step;
    }
    painter->drawImage(QPoint(startXstartY)image);
    delete painter;
}             
              
void KeyEvent::keyPressEvent(QKeyEvent *event)
{
if(event->modifiers() == Qt::ControlModifier)
{
if(event->key() == Qt::Key_Left)
{
startX=(startX-1<0)?startX:startX-1;

}
if(event->key() == Qt::Key_Right)
{
startX=(startX+1+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+1+image.height()>height)?startY:startY+1;

}
}
else
{
startX=startX-startX%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+step+image.height()>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();
}
}
    update();
}              
                                                                             
void KeyEvent::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    drawPix();
    painter.drawPixmap(QPoint(00)*pix);
}

void KeyEvent::resizeEvent(QResizeEvent *event)
{
    width=size().width();
    height=size().height();
    startX=(startX + image.width()>width)?width-image.width():startX;
    startY=(startY+image.height()>height)?height-image.height():startY;
    if (pix) {
        delete pix;
        pix = NULL;
    }
    pix = new QPixmap(widthheight);
    update();
}

评论

共有 条评论