QPainter 基礎繪圖


調用QPainter的接口來繪制一些基本的圖形

頭文件:

 1 #include <QMainWindow>
 2 #include <QPainter>
 3 
 4 namespace Ui {
 5 class MainWindow;
 6 }
 7 
 8 class MainWindow : public QMainWindow
 9 {
10     Q_OBJECT
11 
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 <QRegExp>
 2 #include <QPixmap>
 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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM