QT实现图片的滚轮缩放、框选放大、拖拽移动

序言

QT用来做界面程序是真的方便,它本身封装了很多类库,需要的时候直接拿来用就行。
QT的类继承关系网上有很多相关的文章,这里不做赘述。但是简单的界面程序最主要的继承关系还是如下图:
箭头的意思是派生
可以将QT的类库分为两个大类,对象和事件。对象之间的信息传递为信号,执行操作的是槽函数。事件的发生则是人机交互的过程,比如鼠标点击、鼠标滚轮滑动、键盘按键等。

算了不多说了,直接上效果图:
程序运行效果图

太累了,直接上程序程序代码:

#ifndef DRAW_H
#define DRAW_H

#include <QWidget>
#include "map.h"
#include "drawpixmap.h"
#include "coordtrans.h"
#include <QMouseEvent>
#include <QPushButton>

class Draw : public QWidget
{
    
    
    Q_OBJECT

public:
    Draw(Map& rMap);
    ~Draw();

    // Member Funcitons
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent *event);
    void wheelEvent(QWheelEvent *event);


    void setupUi(int iWidth, int iHeight);


    // Properties
    int _iAction;
    QPushButton* _pBtn1;
    QPushButton* _pBtn2;
    QPushButton* _pBtn3;
    QPushButton* _pBtn4;
    QWidget* _pWgt;
    QPixmap* _pPixmap;
    QPixmap* _pPixmap2;
    QPainter* _pPainter;
    QPoint _beginPos;
    QPoint _endPos;

    bool _bIsLefButtonDown;
    int _iXOffset,_iYOffset;
    int _iXAllOffset,_iYAllOffset;

    DrawPixmap _pDrawPixmap;

    Box* pBox;  // 用来调用Box类的成员函数
    CoordTrans _Transfer;
    Map* pMap;

    int _mousePos[4];
    QPoint _PointBegin;
    QPoint _PointEnd;


public slots:
    void frameAmpliffication();
    void recover();
    void dragMove();
    void loadFile();





};
#endif // DRAW_H

这是draw,h头文件,源文件代码如下:

#include "draw.h"
#include <QPainter>
#include <iostream>
#include <QBitmap>

Draw::Draw(Map& rMap) :_pDrawPixmap(&rMap)
{
    
    
    setupUi(800,600);
    this->pMap = &rMap;
    _pPixmap = new QPixmap(this->width(), this->height()-30);
    _pPixmap->fill(Qt::white);
    _Transfer.Initialize(pMap->_dBox, this->width(), this->height()-30);
    _pPixmap2=new QPixmap(this->width(),this->height()-30);
    _pPixmap2->fill(Qt::transparent);

    _iXOffset=0,_iYOffset=0;
    _iXAllOffset=0;_iYAllOffset=0;
    _iAction=0;
}

Draw::~Draw()
{
    
    
}

void Draw::paintEvent(QPaintEvent*)
{
    
    
    QPainter painter(this);

    if(_iAction==1)
    {
    
    // 框选放大按钮点击
        _pDrawPixmap.update(&_Transfer, _pPixmap,0,0);
        painter.drawPixmap(0,30,this->width(),this->height()-30,*_pPixmap/*, -_iXAllOffset, -_iYAllOffset,this->width(),this->height()-30*/);
        painter.drawPixmap(0,30,this->width(),this->height()-30,*_pPixmap2);
    }
    else if(_iAction==0 || _iAction==2)
    {
    
    // 初始状态或者恢复按钮点击
        _pDrawPixmap.update(&_Transfer, _pPixmap,0,0);
        painter.drawPixmap(0,30,this->width(),this->height()-30,*_pPixmap/*, -_iXAllOffset, -_iYAllOffset,this->width(),this->height()-30*/);
    }
    if(_iAction==3)
    {
    
    // 拖拽移动按钮点击
        _pPixmap->fill(Qt::white);
        _pDrawPixmap.update(&_Transfer, _pPixmap,_iXAllOffset,_iYAllOffset);
        painter.drawPixmap(0,30,*_pPixmap );
    }
}

void Draw::mousePressEvent(QMouseEvent* event)
{
    
    
    _beginPos.setX(event->x());
    _beginPos.setY(event->y()-30);

    _mousePos[0] = event->x();
    _mousePos[1] = event->y()-30;
    _bIsLefButtonDown=true;
    if(_iAction==1)
    {
    
        }
    else if(_iAction==2)
    {
    
        }
    else if(_iAction==3)
    {
    
    
        _iXAllOffset+=_iXOffset;
        _iYAllOffset+=_iYOffset;
    }
}

void Draw::mouseMoveEvent(QMouseEvent *event)
{
    
    
    _endPos.setX(event->pos().x());
    _endPos.setY(event->pos().y()-30);

    if(_bIsLefButtonDown)
    {
    
    
        if(_iAction==1)
        {
    
    
            QPainter painter2(_pPixmap2);
            _pPixmap2->fill(Qt::transparent);
            painter2.drawRect(QRect(_beginPos,_endPos));
            update();
        }
        if(_iAction==3)
        {
    
    
            int tempx=_iXOffset;
            int tempy=_iYOffset;
            _iXOffset = _endPos.x()-_beginPos.x();
            _iYOffset = _endPos.y()-_beginPos.y();

            _iXAllOffset += (_iXOffset-tempx);
            _iYAllOffset += (_iYOffset-tempy);
            update();
        }
    }
}

void Draw::mouseReleaseEvent(QMouseEvent* event)
{
    
    
    if(_iAction==1)
    {
    
    
        if(event->button() == Qt::LeftButton)
        {
    
    
            _bIsLefButtonDown=false;
            _mousePos[2] = event->x();
            _mousePos[3] = event->y()-30;

            double dxb,dyb,dxe,dye;

            _Transfer.C2M(_mousePos[0],_mousePos[1],dxb,dyb);
            _Transfer.C2M(_mousePos[2],_mousePos[3],dxe,dye);
            double ChosenArea[4]={
    
    dxb,dyb,dxe,dye};

            _Transfer.Initialize(ChosenArea,this->width(),this->height()-30);
            _pPixmap->fill(Qt::white);

            _pPixmap2->fill(Qt::transparent);
            update();
        }
    }
    else if(_iAction==2)
    {
    
    }
    else if(_iAction==3)
    {
    
        }
    else if(_iAction==4)
    {
    
    }
}

void Draw::wheelEvent(QWheelEvent *event)
{
    
    
    if(event->delta()>0)
    {
    
    

    }
}

void Draw::setupUi(int iWidth, int iHeight)
{
    
    
    this->setWindowTitle("MyAPP");
    this->resize(iWidth,iHeight);

    _pBtn1=new QPushButton("框选放大",this);
    _pBtn1->setGeometry(0,0,80,30);
    connect(_pBtn1,SIGNAL(clicked()),this,SLOT(frameAmpliffication()));

    _pBtn2=new QPushButton("恢复",this);
    _pBtn2->setGeometry(85,0,80,30);
    connect(_pBtn2,SIGNAL(clicked()),this,SLOT(recover()));

    _pBtn3=new QPushButton("拖拽移动",this);
    _pBtn3->setGeometry(170,0,80,30);
    connect(_pBtn3,SIGNAL(clicked()),this,SLOT(dragMove()));

    _pBtn4=new QPushButton("加载文件",this);
    _pBtn4->setGeometry(255,0,80,30);
    connect(_pBtn4,SIGNAL(clicked()),this,SLOT(loadFile()));
}

void Draw:: frameAmpliffication()
{
    
    
    this->setCursor(Qt::ArrowCursor);
    _iAction=1;
}
void Draw:: recover()
{
    
    
    _iAction=2;
    this->setCursor(Qt::ArrowCursor);
    _pPixmap->fill(Qt::white);
    _Transfer.Initialize(pMap->_dBox,this->width(),this->height()-30);
    update();
}
void Draw:: dragMove()
{
    
    
    _iAction=3;
    if(this->cursor().pos().y()>30)
        this->setCursor(Qt::OpenHandCursor);
}
void Draw:: loadFile()
{
    
    
    _iAction=4;
}

有不懂的留言,我时常来逛。太累了,睡觉了。

猜你喜欢

转载自blog.csdn.net/GeomasterYi/article/details/106446674