Preface
QT is really convenient to be used as an interface program. It encapsulates a lot of libraries, and you can use it directly when you need it.
There are many related articles on QT's class inheritance relationship on the Internet, so I won't go into details here. But the most important inheritance relationship of a simple interface program is as follows:
QT's class library can be divided into two major categories, objects and events. Information between objects is passed as signals, and operations are performed by slot functions. The occurrence of events is the process of human-computer interaction, such as mouse clicks, mouse wheel sliding, keyboard keys, and so on.
Forget it, let’s go directly to the renderings:
Too tired, go directly to the program code:
#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
This is the draw, h header file, the source file code is as follows:
#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;
}
There are unintelligible messages, I often visit. Too tired to sleep.