zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    screenonclick类
    凶残
    接近毁灭的状态
    在python3中,items()进行了优化
    python中两个字典(dict)合并
    Python代码中os.path常见属性的相关介绍 from 路人甲
    什么是pyc文件
    wget
    Python的字典的items(), keys(), values()
    利用securecrt在linux与windows之间传输文件
  • 原文地址:https://www.cnblogs.com/tyche116/p/8522462.html
Copyright © 2011-2022 走看看