博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
QPainter 基础绘图
阅读量:6205 次
发布时间:2019-06-21

本文共 3602 字,大约阅读时间需要 12 分钟。

调用QPainter的接口来绘制一些基本的图形

头文件:

1 #include 
2 #include
3 4 namespace Ui { 5 class MainWindow; 6 } 7 8 class MainWindow : public QMainWindow 9 {10 Q_OBJECT11 12 public:13 explicit MainWindow(QWidget *parent = 0);14 ~MainWindow();15 16 private:17 void paintEvent(QPaintEvent *event);18 19 private:20 Ui::MainWindow *ui;21 int m_type;22 };

 

源文件:

1 #include 
2 #include
3 #include "mainwindow.h" 4 #include "ui_mainwindow.h" 5 6 MainWindow::MainWindow(QWidget *parent) : 7 QMainWindow(parent), 8 ui(new Ui::MainWindow), 9 m_type(1)10 {11 ui->setupUi(this);12 }13 14 MainWindow::~MainWindow()15 {16 delete ui;17 }18 19 void MainWindow::paintEvent(QPaintEvent *event)20 {21 Q_UNUSED(event)22 QPainter painter(this);23 24 if(m_type == 1) // 绘制文本25 {26 painter.setPen(Qt::blue);27 QFont font;28 font.setFamily("Microsoft YaHei");29 font.setPointSize(50);30 font.setItalic(true);31 font.setBold(true);32 painter.setFont(font);33 painter.drawText(rect(), Qt::AlignCenter, QString::fromLocal8Bit("Qt"));34 }35 else if(m_type == 2) // 绘制直线36 {37 painter.setRenderHint(QPainter::Antialiasing, true);38 painter.setPen(Qt::blue);39 painter.drawLine(QPointF(0, height()), QPointF(width() / 2, height() / 2));40 }41 else if(m_type == 3) // 绘制矩形42 {43 painter.setRenderHint(QPainter::Antialiasing, true);44 painter.setPen(QPen(Qt::blue, 2));45 painter.setBrush(Qt::green);46 painter.drawRect(50, 50, 160, 100);47 }48 else if(m_type == 4) // 绘制弧线49 {50 QRectF rect(90.0, 90.0, 80.0, 90.0);51 int startAngle = 30 * 16;52 int spanAngle = 120 * 16;53 painter.setRenderHint(QPainter::Antialiasing, true);54 painter.setPen(QPen(Qt::blue, 2));55 painter.drawArc(rect, startAngle, spanAngle);56 57 }58 else if(m_type == 5) // 绘制椭圆,绘制圆59 {60 painter.setRenderHint(QPainter::Antialiasing, true);61 painter.setPen(QPen(Qt::blue, 2));62 painter.drawEllipse(QPointF(120, 160), 50, 20);63 painter.setBrush(Qt::green);64 painter.drawEllipse(QPointF(120, 140), 40, 40);65 }66 else if(m_type == 6) // 绘制多边形67 {68 painter.setRenderHint(QPainter::Antialiasing, true);69 painter.setPen(Qt::blue);70 static const QPointF points[6] = {QPointF(10, 40), QPointF(60, 190), QPointF(110, 160), QPointF(220, 180), QPointF(270, 120), QPointF(220, 280)};71 painter.drawPolygon(points, 6);72 }73 else if(m_type == 7) // 绘制图片74 {75 painter.setRenderHint(QPainter::Antialiasing, true);76 painter.drawPixmap(rect(), QPixmap(":/images/pic.png"));77 }78 else if(m_type == 8) // 绘制圆角矩形79 {80 painter.setRenderHint(QPainter::Antialiasing, true);81 painter.setPen(QPen(Qt::blue,2,Qt::SolidLine));82 painter.drawRoundRect(80,80,210,160,50,50);83 }84 else if(m_type == 9) // 绘制扇形85 {86 painter.setRenderHint(QPainter::Antialiasing, true);87 painter.setPen(QPen(Qt::green,2,Qt::SolidLine));88 painter.drawPie(80,80,210,160,0,500);89 }90 else if(m_type == 10) // 绘制弦91 {92 painter.setRenderHint(QPainter::Antialiasing, true);93 painter.setPen(QPen(Qt::green,2,Qt::SolidLine));94 painter.drawChord(80,80,210,160,500,1000);95 }96 }

 

转载于:https://www.cnblogs.com/tyche116/p/8522462.html

你可能感兴趣的文章
iOS之UITraitCollection
查看>>
python函数不同类型参数顺序
查看>>
成佛、远不止渡沧海
查看>>
公司 邮件 翻译 培训 长难句 结课
查看>>
poj3692
查看>>
CAS去掉HTTPS认证
查看>>
两道简单的入门题
查看>>
(转)工厂方法模式
查看>>
SVN设置必须锁定
查看>>
BZOJ3387栅栏行动
查看>>
采购订单单价金额屏蔽
查看>>
3.1 采购管理规划
查看>>
leetcode 145. Binary Tree Postorder Traversal
查看>>
LeetCode:461. Hamming Distance
查看>>
ubuntu防火墙
查看>>
Python--day26--复习
查看>>
turtle 20秒画完小猪佩奇“社会人”
查看>>
easyui 布局之window和panel一起使用时,拉动window宽高时panel不跟随一起变化
查看>>
poj1703
查看>>
G.点我
查看>>