哎!和上次一样,最后还是我自己来回答我自己的问题,虽然我现在还无法完全的回答我自己提的问题,但是呢,功能的实现还是差不多的,不过现在还有点问题,就是图像在移动时候,会出现图像的变形,这个应该是坐标计算的问题,这个问题的解决应该只是时间和个人意愿的问题。不知道我这样说,对不对啊!
下面呢我把我自己的代码先贴出来,暂时还没有时间对代码进行注释,等后面有空再说吧!如果哪位大牛看到哪里有不妥的话一定要留言给我,我好及时的改正,以免误导别人!
——————————————————————————————————————————————-
还有一点我得说清楚喽,就是这个问题只是下面程序的一个问题,下面整个程序实现的功能是:
实现读取一幅图片,实现图片的在规定区域的显示、放大、缩小、拖动!看着很简单样子,我一开始弄的可费劲了,问了好几天,都没人理我!
——————————————————————————————————————————————–
///
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPixmap>
#include <QImage>
#include <QMouseEvent>
#include <QGraphicsView>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
void loadimg_signal();
void moveimg_signal();
void down_signal();
void up_signal();
void mousepress_signal();
public:
QImage img;
static qreal x;
static qreal y;
static qreal x0;
static qreal y0;
static qreal x_img;
static qreal y_img;
static bool mousemove_flage;
static bool mousepress_flage;
static qreal width_img;
static qreal height_img;
static qreal x_s;
static qreal y_s;
static qreal w_s;
static qreal h_s;
static qreal x_temp;
static qreal y_temp;
//static int num;
private slots:
void on_pushButton_clicked();
void paintEvent(QPaintEvent *);
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
private:
Ui::MainWindow *ui;
void mouseMoveEvent(QMouseEvent *m);
void mousePressEvent(QMouseEvent *);
//QGraphicsView *view;
};
#endif // MAINWINDOW_H
//
mainwindow.cpp
//
#include “mainwindow.h”
#include “ui_mainwindow.h”
#include <QGraphicsView>
#include <QPainter>
#include <QHBoxLayout>
qreal MainWindow::x=0; //鼠标在主视窗的坐标
qreal MainWindow::y=0;
qreal MainWindow::x0=0; //图片坐标原点在主视窗坐标系的坐标
qreal MainWindow::y0=0;
qreal MainWindow::x_img=0; //鼠标在图片坐标系的坐标
qreal MainWindow::y_img=0;
bool MainWindow::mousemove_flage=0; //鼠标移动标示符
bool MainWindow::mousepress_flage=0; //鼠标点击标示符
qreal MainWindow::width_img=100;
qreal MainWindow::height_img=100;
qreal MainWindow::x_s=0.0;
qreal MainWindow::y_s=0.0;
qreal MainWindow::w_s=1280.0;
qreal MainWindow::h_s=960.0;
qreal MainWindow::x_temp=0.0;
qreal MainWindow::y_temp=0.0;
//int MainWindow::num=0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
img.load(“C:/Documents and Settings/67.bmp”);
width_img=img.width()/3;
height_img=img.height()/3;
connect(this,SIGNAL(loadimg_signal()),SLOT(paintEvent(QPaintEvent *)));
update();
}
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter painte(this);
QRectF pt(70.0,50.0,320.0,240.0);
QRectF source(x_s-3*x0,y_s-3*y0,w_s-x0*.5,h_s-y0*0.5);
painte.drawImage(pt,img,source);
update();
}
void MainWindow::mousePressEvent(QMouseEvent *h)
{
if(mousemove_flage)
{
x=h->pos().x();
y=h->pos().y();
x_temp=x;
y_temp=y;
mousepress_flage=1;
connect(this,SIGNAL(mousepress_signal()),SLOT(paintEvent(QPaintEvent *)));
update();
}
}
void MainWindow::mouseMoveEvent(QMouseEvent *m)
{
if(mousemove_flage&&mousepress_flage)
{
x=m->pos().x();
y=m->pos().y();
x0=x-x_temp;
y0=y-y_temp;
//x_s=x_s-x0;
//y_s=y_s-y0;
update();
}
}
void MainWindow::on_pushButton_2_clicked()
{
mousemove_flage=!mousemove_flage;
}
void MainWindow::on_pushButton_3_clicked()
{
if((w_s<1280.0)&&(h_s<960.0))
{
x_s=x_s-1280*5/100;
y_s=y_s-960*5/100;
h_s=h_s+960*10/100;
w_s=w_s+1280*10/100;
connect(this,SIGNAL(down_signal()),SLOT(paintEvent(QPaintEvent*)));
update();
}
}
void MainWindow::on_pushButton_4_clicked()
{
x_s=x_s+1280*5/100;
y_s=y_s+960*5/100;
h_s=h_s-960*10/100;
w_s=w_s-1280*10/100;
connect(this,SIGNAL(up_signal()),SLOT(paintEvent(QPaintEvent*)));
update();
}
///
main.cpp
///
#include <QtGui/QApplication>
#include “mainwindow.h”
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
///
mainwindow.ui
///
最后的效果
好了,先就这样吧,等有空再来改改吧!
FROM: http://www.zhihu.com/question/20550860/answer/15462219