zoukankan      html  css  js  c++  java
  • QT_SVG格式图片浏览器_源代码下载_详细注释

    源代码链接: http://pan.baidu.com/s/1pKA5Vcv 密码: ib2x

    注:SVG格式图片特点: 1. 文件小 2. 图像中文字独立于图像, 可以编辑,可搜索. 3.没有字体限制 4.可以任意缩放而不破坏图像清晰度和细节

    下面实现一个SVG图片浏览器.

    显示效果如图(放大超过窗口大小时, 可以通过滚动条拖曳查看图片)

    1.

    2. (如图,可以任意放大,而不失真)

    源代码如下:

    注: 1.在实现代码前 需要现在工程文件“.pro”中加入 QT += svg

         2.新建项目“SVGTest”,基类选择“QMainWindow", 类名默认---实现主窗口区域

         3.新建c++Class文件“SVGWindow”,基类输入“QScrollArea"(手工添加),类名为"SvgWindow"--实现一个带滚动条显示区域

          4.新建c++Class文件 “SvgWidget”,基类输入“QSvgWidget”(手工添加), 类名为“SVGWidget”--实现具体的SVG图片显示工作

    //svgwidget.h

     1 /***
     2  * 实现显示SVG图片的函数所在的文件.
     3  */
     4 #ifndef SVGWIDGET_H
     5 #define SVGWIDGET_H
     6 
     7 #include <QtSvg/QtSvg>
     8 #include <QtSvg/QSvgWidget>
     9 #include <QtSvg/QSvgRenderer>
    10 
    11 class SvgWidget : public QSvgWidget
    12 {
    13     Q_OBJECT
    14 public:
    15     SvgWidget(QWidget *parent = 0);
    16     //响应鼠标的滚动事件,使SVG图片能够通过鼠标滚轮的 滚动 进行 缩放
    17     void wheelEvent (QWheelEvent *);
    18 private:
    19     //渲染器
    20     QSvgRenderer *render;           //用于图片显示尺寸 的 确定
    21 };
    22 
    23 #endif // SVGWIDGET_H

    //svgwidget.cpp

     1 #include "svgwidget.h"
     2 
     3 SvgWidget::SvgWidget(QWidget *parent)
     4     : QSvgWidget(parent)
     5 {
     6     //获得本窗体的QSvgRenderer对象
     7     render = renderer ();
     8 }
     9 
    10 //滚轮响应事件,使SVG图片能够鼠标滚动滑轮进行缩放
    11 void SvgWidget::wheelEvent (QWheelEvent *e)
    12 {
    13     //diff的值表示每次滚轮滑动一定的值,图片大小改变的比例
    14     const double diff = 0.1;
    15     /***
    16      * 下面三行代码: 用于获取图片显示区的尺寸大小,以便进行下一步的缩放操作
    17      */
    18     QSize size = render->defaultSize ();
    19     int width = size.width ();
    20     int height = size.height ();
    21     /***
    22      * 利用QWheelEvent的delta()函数获得滚轮的距离值,通过此值来判断滚轮滚动的方向
    23      * delta() > 0 , 则表示滚轮向前(远离用户方向); delta() < 0, 则表示向后滚动
    24      * (Ps: 鼠标滚动事件,滚轮每滚动1°, 相当于移动8°;而常见的滚轮鼠标拨动一下滚动角度为15°
    25      * 因此,滚轮拨动一下相当于移动了 120(=15*8)
    26      */
    27     if (e->delta () > 0)
    28     {
    29         //对图片的长, 宽值进行处理, 放大一定的比例
    30         width = int(this->width () + this->width ()*diff);
    31         height = int(this->height () + this->height ()*diff);
    32     }
    33     else
    34     {
    35         //对图片的长,宽进行处理,缩小一定的比例
    36         width = int(this->width () - this->width ()*diff);
    37         height = int(this->height () - this->height ()*diff);
    38     }
    39     //利用新的长,宽值对图片进行resize()操作
    40     resize (width, height);
    41 
    42 }

    //svgwindow.h

     1 /*
     2  * 实现一个带滚动条显示区域的函数所在的文件.
     3  * 使图片在放大到超过主窗口大小时, 能通过拖曳
     4  */
     5 #ifndef SVGWINDOW_H
     6 #define SVGWINDOW_H
     7 
     8 #include <QScrollArea>
     9 #include "svgwidget.h"
    10 
    11 class SvgWindow : public QScrollArea
    12 {
    13     Q_OBJECT
    14 public:
    15     SvgWindow(QWidget *parent = 0);
    16     void setFile(QString);
    17     void mousePressEvent (QMouseEvent *);
    18     void mouseMoveEvent (QMouseEvent *);
    19 private:
    20     SvgWidget *svgWidget;
    21     QPoint mousePressPos;
    22     QPoint scrollBarValueOnMousePress;
    23 };
    24 
    25 #endif // SVGWINDOW_H

    //svgwindow.cpp

     1 #include "svgwindow.h"
     2 
     3 SvgWindow::SvgWindow(QWidget *parent)
     4     : QScrollArea(parent)
     5 {
     6     svgWidget = new SvgWidget;
     7     setWidget (svgWidget);       //设置滚动条
     8 }
     9 
    10 //当主窗口对文件进行了选择或修改时, 将调用setFile()函数设置新的文件
    11 void SvgWindow::setFile (QString fileName)
    12 {
    13     //将新的SVG文件加载到svgWidget中显示
    14     svgWidget->load (fileName);
    15     //返回渲染器,去显示控件的内容
    16     QSvgRenderer *render = svgWidget->renderer ();
    17     //使svgWidget窗体按SVG图片的默认尺寸进行显示
    18     svgWidget->resize (render->defaultSize ());
    19 
    20 }
    21 
    22 /***
    23  * 鼠标键按下时,mousePressPos和scrollBarValuesOnMousePress进行初始化,QScrollArea的horizonalScrollBar()
    24  * 和verticalScrollBar()函数可以分别获得svgWindow的水平滚动条和垂直滚动条
    25  */
    26 void SvgWindow::mousePressEvent (QMouseEvent *event)
    27 {
    28     mousePressPos = event->pos ();         //记下当前事件位置
    29     scrollBarValueOnMousePress.rx () = horizontalScrollBar ()->value ();
    30     scrollBarValueOnMousePress.ry () = verticalScrollBar ()->value ();
    31     //设置事件对象的接受信号,表明事件接受器希望事件,意外事件传到父部件
    32     event->accept ();
    33 }
    34 
    35 /***
    36  * 当鼠标按下并拖曳鼠标时, 触发mouseMoveEvent()函数,通过滑动条的位置设置实现图片图片拖曳的效果
    37  */
    38 void SvgWindow::mouseMoveEvent (QMouseEvent *event)
    39 {
    40     //对水平滑动条的新位置进行设置
    41     //新水平滑动条位置 = (滑块当前位置 - 鼠标即时返回的位置)(即:滑块移动位置) + 鼠标之前记下的事件位置
    42     horizontalScrollBar ()->setValue (scrollBarValueOnMousePress.x () - event->pos ().x () + mousePressPos.x ());
    43     //对垂直滑动条的新位置进行设置
    44     verticalScrollBar ()->setValue (scrollBarValueOnMousePress.y () - event->pos ().y () + mousePressPos.y ());
    45     horizontalScrollBar ()->update ();
    46     verticalScrollBar ()->update ();
    47     event->accept ();
    48 }

    //mainwindow.h

     1 #ifndef MAINWINDOW_H
     2 #define MAINWINDOW_H
     3 
     4 #include <QMainWindow>
     5 #include "svgwindow.h"
     6 
     7 class MainWindow : public QMainWindow
     8 {
     9     Q_OBJECT
    10 
    11 public:
    12     MainWindow(QWidget *parent = 0);
    13     ~MainWindow();
    14     void createMenu();
    15 public slots:
    16     void slotOpenFile();
    17 private:
    18     SvgWindow *svgWindow;              //用于调用相关函数传递选择的文件名
    19 };
    20 
    21 #endif // MAINWINDOW_H

    //mainwindow.cpp

     1 #include "mainwindow.h"
     2 
     3 MainWindow::MainWindow(QWidget *parent)
     4     : QMainWindow(parent)
     5 {
     6     setWindowTitle (tr("SVG Viewer"));
     7     createMenu ();
     8     svgWindow = new SvgWindow;
     9     setCentralWidget (svgWindow);
    10 }
    11 
    12 //创建菜单栏
    13 void MainWindow::createMenu ()
    14 {
    15     //添加“文件”菜单条
    16     QMenu *fileMenu = menuBar ()->addMenu (tr("文件"));
    17     QAction *openAct = new QAction(tr("打开"), this);
    18     connect (openAct, SIGNAL(triggered(bool)), this, SLOT(slotOpenFile()));
    19     //添加"打开"菜单项
    20     fileMenu->addAction(openAct);
    21 }
    22 
    23 //通过标准文件对话框选择SVG文件, 并调用SvgWindow的setFile()函数将选择的文件名传递给svgWindow进行显示
    24 void MainWindow::slotOpenFile ()
    25 {
    26     QString name = QFileDialog::getOpenFileName (this, "打开", "/", "svg file(*.svg)");
    27     svgWindow->setFile (name);
    28 }
    29 
    30 MainWindow::~MainWindow()
    31 {
    32 
    33 }

    //main.cpp

     1 #include "mainwindow.h"
     2 #include <QApplication>
     3 
     4 int main(int argc, char *argv[])
     5 {
     6     QApplication a(argc, argv);
     7     MainWindow w;
     8     w.show();
     9 
    10     return a.exec();
    11 }
  • 相关阅读:
    hi.baidu.com 百度流量统计
    Autofac is designed to track and dispose of resources for you.
    IIS Manager could not load type for module provider 'SharedConfig' that is declared in administration.config
    How to create and manage configuration backups in Internet Information Services 7.0
    定制swagger的UI
    NSwag在asp.net web api中的使用,基于Global.asax
    NSwag Tutorial: Integrate the NSwag toolchain into your ASP.NET Web API project
    JS变量对象详解
    JS执行上下文(执行环境)详细图解
    JS内存空间详细图解
  • 原文地址:https://www.cnblogs.com/douzujun/p/5792314.html
Copyright © 2011-2022 走看看