位图和矢量图:

Bitmap:

  • Usually a larger file size
  • Cannot be enlarged into a higher resolution as the image quality will be affected
  • Used to display complex images with many colors, such as photographs

Vector:

  • Very small in file size
  • Graphics can be resized without affecting the image quality
  • Only a limited amount of color can be applied to each shape (single color, gradient, or pattern)
  • Complex shapes require high-processing power to be generated

新建基于Widget的应用程序,继承于QMainWindow

 MainWindow.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPainter>
#include <QMenuBar>
#include <QtSvg/QSvgGenerator>
#include <QFileDialog>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = );
    ~MainWindow();

virtual void paintEvent(QPaintEvent *event);

public slots:
    void actionSaveAsSVG();

private:
    void paintAll(QSvgGenerator *generator = nullptr);
};

#endif // MAINWINDOW_H

 MainWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
 
#include "MainWindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QMenu *fileMenu = menuBar()->addMenu(QObject::tr("&File"));
    fileMenu->addAction(QObject::tr("Save As Svg"), this, &MainWindow::actionSaveAsSVG);
}

MainWindow::~MainWindow()
{

}

void MainWindow::paintEvent(QPaintEvent *event)
{
    paintAll();
}

void MainWindow::actionSaveAsSVG()
{
    QString filePath = QFileDialog::getSaveFileName(this, "Save SVG",

"", "SVG files (*.svg)");
    if (filePath == "")
        return;
    QSvgGenerator generator;
    generator.setFileName(filePath);
    generator.setSize(QSize(this->width(), this->height()));
    generator.setViewBox(QRect(, this->width(), this->height()));
    generator.setTitle("SVG Example");
    generator.setDescription("This SVG file is generated by Qt.");
    paintAll(&generator);
}

void MainWindow::paintAll(QSvgGenerator *generator)
{
    QPainter painter;
    if (generator)
        painter.begin(generator);
    else
        painter.begin(this);

// Draw Text
, QFont::Bold));
    painter.drawText(QPoint(), "Hello Qt");
    // Draw Line
));
    // Draw Rectangle
    painter.setBrush(Qt::BDiagPattern);
    painter.drawRect(QRect());
    // Draw Ellipse
    QPen ellipsePen;
    ellipsePen.setColor(Qt::red);
    ellipsePen.setStyle(Qt::DashDotLine);
    painter.setPen(ellipsePen);
    painter.drawEllipse(QPoint();
    // Draw Rectangle
    QPainterPath rectPath;
    rectPath.addRect(QRect());
    painter.setPen(QPen(Qt::red, , Qt::DashDotLine, Qt::FlatCap,
                        Qt::MiterJoin));
    painter.setBrush(Qt::yellow);
    painter.drawPath(rectPath);
    // Draw Ellipse
    QPainterPath ellipsePath;
    ellipsePath.addEllipse(QPoint();
    painter.setPen(QPen(QColor(, Qt::SolidLine,
                        Qt::FlatCap, Qt::MiterJoin));
    painter.setBrush(QColor());
    painter.drawPath(ellipsePath);
    painter.end();
}

 Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
#include "MainWindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

MainWindow w;
    w.resize();
    w.show();

return a.exec();
}

编译运行

另存为svg文件

最新文章

  1. 用crontab跑定时任务[转]
  2. JS,JQ点击事件
  3. Oracle -&gt;&gt; 连续聚合
  4. Quartz Scheduler 开发指南(1)
  5. springMVC+freemarker中Could not resolve view with name... 问题解决
  6. redundant 行记录格式
  7. python模块之json序列化
  8. android 休眠唤醒机制分析(二) — early_suspend
  9. Chapter 4. Using the Gradle Command-Line 使用gradle命令行
  10. bootstrap modal 弹出效果
  11. C语言链表的建立、插入和删除
  12. Spring学习之实例化bean的三种方式
  13. IIC_slaver 的仿真之路
  14. 普通PC安装ESXi5.5以及以上的方法
  15. 一本通1609【例 4】Cats Transport
  16. __getitem__()、__setitem__()与__delitem__()
  17. 二、基于kubernetes构建Docker集群环境实战
  18. HDU1007(最近点对)
  19. Scala学习之路 (六)Scala的类、对象、继承、特质
  20. Ubuntu忘记root密码的解决方法

热门文章

  1. 9.consul获取服务实例,调用测试
  2. 如何使用docker部署springcloud???
  3. hibernate关联关系 (多对多)
  4. mysql 8创建远程访问用户以及连接mysql速度慢的解决方法
  5. 洛谷P5020 货币系统
  6. shell equal
  7. 远程windows
  8. git使用问题与技巧
  9. JS 中判断数据类型是否为 null、undefined 或 NaN
  10. Python3注解+可变参数实现