QT realiza el zoom de la rueda de desplazamiento, el zoom de selección de fotogramas, el arrastrar y mover imágenes

Prefacio

QT es realmente conveniente para ser utilizado como un programa de interfaz, encapsula muchas bibliotecas y puede usarlo directamente cuando lo necesite.
Hay muchos artículos relacionados sobre la relación de herencia de clases de QT en Internet, por lo que no entraré en detalles aquí. Pero la relación de herencia más importante de un programa de interfaz simple es la siguiente:
Flecha significa derivar
la biblioteca de clases de QT se puede dividir en dos categorías principales, objetos y eventos. La información entre objetos se transmite como señales y las operaciones se realizan mediante funciones de intervalo. La ocurrencia de eventos es el proceso de interacción humano-computadora, como clics del mouse, deslizamiento de la rueda del mouse, teclas del teclado, etc.

Olvídalo, vayamos directamente a las representaciones:
Diagrama de efectos de ejecución del programa

Demasiado cansado, vaya directamente al código del programa:

#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

Este es el archivo de encabezado draw, h, el código del archivo fuente es el siguiente:

#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;
}

Hay mensajes ininteligibles, los visito a menudo. Demasiado cansado para dormir.

Supongo que te gusta

Origin blog.csdn.net/GeomasterYi/article/details/106446674
Recomendado
Clasificación