头文件:
#ifndef SVGWIDGET_H
#define SVGWIDGET_H
#include <QSvgWidget>
#include <QWheelEvent>
#include <QSvgRenderer>
#include "ui_svgwidget.h"
class SvgWidget : public QSvgWidget
{
Q_OBJECT
public:
SvgWidget(QWidget *parent = 0);
~SvgWidget();
void wheelEvent(QWheelEvent *event);
void resizeEvent(QResizeEvent * event);
private:
Ui::SvgWidget ui;
};
#endif // SVGWIDGET_H
源码文件:
#include "svgwidget.h"
SvgWidget::SvgWidget(QWidget *parent)
: QSvgWidget(parent)
{
ui.setupUi(this);
QString name = "C:/Users/Administrator/Desktop/pic001.svg";
load(name);
resize(this->size());
}
SvgWidget::~SvgWidget()
{
}
void SvgWidget::resizeEvent(QResizeEvent * event)
{
resize(this->size());
}
void SvgWidget::wheelEvent(QWheelEvent *event)
{
const double diff = 0.1;//每次滚轮滚动一定的值,图片大小改变的比例
QSize size = renderer()->defaultSize();//获取图片的大小,以便进行缩放
int width = size.width();
int height = size.height();
if (event->delta() > 0)//表示delta()>0表示鼠标向前滚动
{
width = int(this->width() + this->width()*diff);//放大/缩小后的宽度
height = int(this->height() + this->height()*diff);
}
else
{
width = int(this->width() - this->width()*diff);
height = int(this->height() - this->height()*diff);
}
resize(width, height);//利用新的长,框值对图片重新显示
}