QT 基本圖形繪製

 QT 基本圖形繪製

 

1.告訴繪製引擎一些東西

       QPainter::Antialiasing 在可能的狀況下,反鋸齒
       QPainter::TextAntialiasing 在可能的狀況下,文字反鋸齒
       QPainter::SmoothPixmapTransform   採用平滑的Pixmap變換算法css

2.繪製函數

       

3.風格

   1)畫刷風格
 
2)畫筆風格
3)畫筆鏈接點風格
4)頂端風格

4.繪圖前的準備

       1  若是要在繪圖設備(通常爲窗口部件)上繪圖,只需建立一個QPainter,再將指針傳到該設備中。算法

          例如:編程

void MyWidget::paintEvent(QPaintEvent *event)架構

{框架

    QPainter painter(this);函數

}佈局

      2  經常使用的初始化:畫筆、畫刷、字體字體

 

       畫筆:用來畫線和邊緣。它包含顏色、寬度、線性、拐點風格以及連線風格。優化

       畫刷:用來填充幾何圖形的圖案。它通常由顏色和風格組成,但同時也能夠是紋理(一個不斷重複的圖像)或者是一個漸變。this

       字體:用來繪製文字。字體有不少屬性,包括字體族和磅值大小。

5.具體的實例

            經過創建一個畫圖面板來了解Qpainter是怎樣繪製圖形的,其中包含兩個類,畫圖的區域PainterArea類和主窗口mainWindow類
       (1)實現PainterArea類
//paintarea.h
#ifndef PAINTAREA_H
#define PAINTAREA_H
 
  
#include <QWidget>
#include<QPen>
#include<QPaintEvent>
 
  
class PaintArea : public QWidget
{
    Q_OBJECT
 
  
public:
    enum Shape{Line,Rectangle,RoundRect,Ellipse,Polygon,Polyline,Points,Arc,Path,Text,
               Pixmap};
    PaintArea(QWidget * parent=0);
    void setShape(Shape);       //設置形狀
    void setPen(QPen);          //設置畫筆
    void setBrush(QBrush);      //設置畫刷
    void setFillRule(Qt::FillRule);//設置填充模式
    void paintEvent(QPaintEvent *);//重畫事件
private:
    Shape shape;
    QPen pen;
    QBrush brush;
    Qt::FillRule fillRule;
};
 
  
 
  
#endif // PAINTAREA_H
 
//paintarea.cpp

 

#include "paintarea.h"
#include <QPainter>
 
 
PaintArea::PaintArea(QWidget *parent)
    :QWidget(parent)
{
    setPalette(QPalette(Qt::white)); //設置背景顏色
    setAutoFillBackground(true);//設置自動填充背景色
    setMinimumSize(400,400);//設置窗口最下大小
}
 
 
void PaintArea::setShape(Shape s)   //update()更新窗口部件
{
    shape=s;
    update();
}
void PaintArea::setPen(QPen p)
{
    pen=p;
    update();
}
void PaintArea::setBrush(QBrush b)
{
    brush=b;
    update();
}
void PaintArea::setFillRule(Qt::FillRule rule)
{
    fillRule=rule;
    update();
}
 
 
//重畫事件
void PaintArea::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.setPen(pen);
    p.setBrush(brush);
    QRect rect(100,100,250,200);//構造一個矩形
 
 
    static const QPoint points[4]={
        QPoint(100,100),
        QPoint(200,150),
        QPoint(300,250),
        QPoint(150,300)
    };//肯定Points的四個點的座標
 
 
    int startAngle=60*16;
    int spanAngle=180*16;  //爲繪製曲線設置參數變量
 
 
    QPainterPath path;     //QPainterPath爲Qpainter類提供了一個存儲容器,裏面包含了畫的內容和畫的順序,
    path.moveTo(50,150);    //當前位置移動到座標50,150
    path.lineTo(350,150);   //當前位置開始畫直線,終點位置座標350,150
    path.lineTo(100,325);
    path.lineTo(200,50);
    path.lineTo(300,325);
    path.lineTo(50,150);
    path.setFillRule(fillRule);  //設置填充模式
    switch(shape)
    {
        case Line:       //直線
        p.drawLine(rect.topLeft(),rect.bottomRight());  //繪製直線。起點爲矩形左上點,終點爲矩形右下點
        break;
 
 
        case Rectangle: //長方形
        p.drawRect(rect);
        break;
 
 
        case RoundRect:  //圓角方形
        p.drawRoundRect(rect);
        break;
 
 
        case Ellipse:   //橢圓形
        p.drawEllipse(rect);
        break;
 
 
        case Polygon:   //多邊形
        p.drawPolygon(points,4);  //繪製4個頂點多邊形
        break;
 
 
        case Polyline:  //多邊線
        p.drawPolyline(points,4);
        break;
 
 
        case Points:    //點
        p.drawPoints(points,4);
        break;
 
 
        case Arc:   //弧
        p.drawArc(rect,startAngle,spanAngle);  //後面兩個參數分別爲 起始角與跨度角
        break;
 
 
        case Path:  //繪製以前已經畫好的路徑
        p.drawPath(path);
        break;
 
 
        case Text:  //文字
        p.drawText(rect,Qt::AlignCenter,tr("Hello Qt!"));
        break;
 
 
        case Pixmap:    //圖片
        p.drawPixmap(150,150,QPixmap("1.png"));//繪製一個圖像
        break;
 
 
        default:
        break;
    }
}
//前面的橢圓形、直線、長方形、圓角矩形,弧 都是利用矩形爲骨架構成的,具體如何構成參照介紹中的繪製函數中的圖。

 

    (2)實現主窗口類MainWidget

//mainwindow.h頭文件

 

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
 
#include <QWidget>
#include<paintarea.h>
#include <QLabel>
#include <QComboBox>
#include <QSpinBox>
#include<QPushButton>
#include <QGridLayout>
class MainWindow : public QWidget
{
    Q_OBJECT
 
 
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    PaintArea *paintArea;
 
 
    QLabel *shapelabel;//形狀
    QComboBox *shapeComboBox;
 
 
    QLabel *penWidthLable;//畫筆寬度
    QSpinBox *penWidthSpinBox;
 
 
    QLabel *penColorLabel;//畫筆顏色
    QFrame *penColorFrame;
    QPushButton *penColorbtn;//畫筆顏色按鈕
 
 
    QLabel *penStyleLabel;
    QComboBox *penStyleComboBox;//畫筆風格
 
 
    QLabel *penCapLabel;
    QComboBox *penCapComboBox;
 
 
    QLabel *penJoinLabel;
    QComboBox *penJoinComboBox;
 
 
    QLabel *fillLabel;
    QComboBox *fillComboBox;
 
 
    QLabel *brushStyleLabel;
    QComboBox *brushStyleComboBox;
 
 
    QLabel *brushColorlabel;
    QFrame *brushColorFrame;
    QPushButton *brushColorbtn;
 
 
    QGridLayout *settingLayout;
 
 
protected slots:
 
 
    void ShowShape(int);
    void ShowPenWidth(int);
    void ShowPenColor();
    void ShowPenStyle(int);
    void ShowPenCap(int);
    void ShowPenJoin(int);
    void ShowFill();
    void ShowBrushColor();
    void ShowBrush(int);
 
 
};
 
 
#endif // MAINWINDOW_H
//mainwindow.cpp
#include"mainwindow.h"
#include "paintarea.h"
#include<QColorDialog>
 
 
MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent)
{
    paintArea=new PaintArea;
 
 
    shapelabel=new QLabel("形狀:");  //形狀選擇下拉列表框
    shapeComboBox=new QComboBox;
    shapeComboBox->addItem(tr("Line"),PaintArea::Line);
    shapeComboBox->addItem(tr("RoundRect"),PaintArea::RoundRect);
    shapeComboBox->addItem(tr("Ellipse"),PaintArea::Ellipse);
    shapeComboBox->addItem(tr("Polygon"),PaintArea::Polygon);
    shapeComboBox->addItem(tr("Polyline"),PaintArea::Polyline);
    shapeComboBox->addItem(tr("Points"),PaintArea::Points);
    shapeComboBox->addItem(tr("Arc"),PaintArea::Arc);
    shapeComboBox->addItem(tr("Rectangle"),PaintArea::Rectangle);
    shapeComboBox->addItem(tr("Path"),PaintArea::Path);
    shapeComboBox->addItem(tr("Text"),PaintArea::Text);
    shapeComboBox->addItem(tr("Pixmap"),PaintArea::Pixmap);
    connect(shapeComboBox,SIGNAL(activated(int)),this,SLOT(ShowShape(int)));
 
 
    penColorLabel=new QLabel("畫筆顏色:"); //畫筆顏色選擇控件
    penColorFrame=new QFrame;
    penColorFrame->setFrameStyle(QFrame::Panel|QFrame::Sunken);//Panel - QFrame畫一個平板使內容看起來凸起或者凹陷,
                                                                 //QFrame::Sunken - 框架和內容看起來凹陷 
    penColorFrame->setAutoFillBackground(true);
    penColorFrame->setPalette(QPalette(Qt::blue));//設置默認顏色爲藍色
    penColorbtn=new QPushButton;
    connect(penColorbtn,SIGNAL(clicked()),this,SLOT(ShowPenColor()));
 
 
    penWidthLable=new QLabel("畫筆寬度:");//畫筆寬度選擇控件
    penWidthSpinBox=new QSpinBox;
    penWidthSpinBox->setRange(0,20);//設置字寬滑塊的取值範圍
    connect(penWidthSpinBox,SIGNAL(valueChanged(int)),this,SLOT(ShowPenWidth(int)));
 
 
    penStyleLabel=new QLabel; //畫筆風格選擇下拉列表框
    penStyleComboBox=new QComboBox;
    penStyleComboBox->addItem(tr("SolidLine"),Qt::SolidLine);
    penStyleComboBox->addItem(tr("DashLine"),Qt::DashLine);
    penStyleComboBox->addItem(tr("DotLine"),Qt::DotLine);
    penStyleComboBox->addItem(tr("DashDotLine"),Qt::DashDotLine);
    penStyleComboBox->addItem(tr("DashDotDotLine"),Qt::DashDotDotLine);
    penStyleComboBox->addItem(tr("CustomDashLine"),Qt::CustomDashLine);//這段若是報錯,能夠把addItem後面的參數置換爲int類的數字
    connect(penStyleComboBox,SIGNAL(activated(int)),this,SLOT(ShowPenStyle(int)));
 
 
    penCapLabel=new QLabel;  //畫頂端風格選擇下拉列表框
    penCapComboBox=new QComboBox;
    penCapComboBox->addItem(tr("SquareCap"),Qt::SquareCap);
    penCapComboBox->addItem(tr("FlatCap"),Qt::FlatCap);
    penCapComboBox->addItem(tr("RoundCap"),Qt::RoundCap);
    connect(penCapComboBox,SIGNAL(activated(int)),this,SLOT(ShowPenCap(int)));
 
 
    penJoinLabel=new QLabel;//畫筆鏈接點風格選擇下拉列表框
    penJoinComboBox=new QComboBox;
    penJoinComboBox->addItem(tr("BevelJoin"),Qt::BevelJoin);
    penJoinComboBox->addItem(tr("MiterJoin"),Qt::MiterJoin);
    penJoinComboBox->addItem(tr("RoundJoin"),Qt::RoundJoin);
 
 
    fillLabel=new QLabel;//填充模式選擇下拉列表框
    fillComboBox=new QComboBox;
    fillComboBox->addItem(tr("Odd Even"),Qt::OddEvenFill);
    fillComboBox->addItem(tr("Winding"),Qt::WindingFill);
    connect(fillComboBox,SIGNAL(activated(int)),this,SLOT(ShowFill()));
 
 
    brushColorlabel=new QLabel;//畫刷風格選擇下拉列表框
    brushColorFrame=new QFrame;
    brushColorbtn=new QPushButton;
    brushColorFrame->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    brushColorFrame->setAutoFillBackground(true);
    brushColorFrame->setPalette(QPalette(Qt::green));
    connect(brushColorbtn,SIGNAL(clicked()),this,SLOT(ShowBrushColor()));
 
 
    brushStyleLabel=new QLabel; //畫刷風格選擇下拉列表框
    brushStyleComboBox=new QComboBox;
    brushStyleComboBox->addItem(tr("SolidPattern"),1);
    brushStyleComboBox->addItem(tr("Dense1Pattern"),2);
    brushStyleComboBox->addItem(tr("Dense2Pattern"),3);
    brushStyleComboBox->addItem(tr("Dense3Pattern"),4);
    brushStyleComboBox->addItem(tr("Dense4Pattern"),5);
    brushStyleComboBox->addItem(tr("Dense5Pattern"),6);
    brushStyleComboBox->addItem(tr("Dense6Pattern"),7);
    brushStyleComboBox->addItem(tr("Dense7Pattern"),8);
    brushStyleComboBox->addItem(tr("HorPattern"),9);
    brushStyleComboBox->addItem(tr("VerPattern"),10);
    brushStyleComboBox->addItem(tr("CrossPattern"),11);
    brushStyleComboBox->addItem(tr("BDiagPattern"),12);
    brushStyleComboBox->addItem(tr("FDiagPattern"),13);
    brushStyleComboBox->addItem(tr("DiagCrossPattern"),14);
    brushStyleComboBox->addItem(tr("LinearGradientPattern"),15);
    brushStyleComboBox->addItem(tr("ConicalGradientPattern"),16);
    brushStyleComboBox->addItem(tr("RadialGradientPattern"),17);
    brushStyleComboBox->addItem(tr("TexturePattern"),24);
        connect(brushStyleComboBox,SIGNAL(activated(int)),this,SLOT(ShowBrush(int)));
 
 
    settingLayout=new QGridLayout;//畫板佈局
    settingLayout->addWidget(shapelabel,0,0);
    settingLayout->addWidget(shapeComboBox,0,1);
    settingLayout->addWidget(penColorLabel,0,2);
    settingLayout->addWidget(penColorFrame,0,3);
    settingLayout->addWidget(penColorbtn,0,4);
    settingLayout->addWidget(penWidthLable,1,0);
    settingLayout->addWidget(penWidthSpinBox,1,1);
    settingLayout->addWidget(penStyleLabel,1,2);
    settingLayout->addWidget(penStyleComboBox,1,3);
    settingLayout->addWidget(penCapLabel,2,0);
    settingLayout->addWidget(penCapComboBox,2,1);
    settingLayout->addWidget(penJoinLabel,2,2);
    settingLayout->addWidget(penJoinComboBox,2,3);
    settingLayout->addWidget(fillLabel,3,0);
    settingLayout->addWidget(fillComboBox,3,1);
    settingLayout->addWidget(brushColorlabel,3,2);
    settingLayout->addWidget(brushColorFrame,3,3);
    settingLayout->addWidget(brushColorbtn,3,4);
    settingLayout->addWidget(brushStyleLabel,4,0);
    settingLayout->addWidget(brushStyleComboBox,4,1);
    QVBoxLayout *mainLayout=new QVBoxLayout(this);
    mainLayout->addLayout(settingLayout);
    mainLayout->addWidget(paintArea);
    ShowShape(shapeComboBox->currentIndex());
}
//如下就是各類槽函數了
void MainWindow::ShowShape(int value)
{
    PaintArea::Shape shape=PaintArea::Shape(shapeComboBox->itemData(value,Qt::UserRole).toInt());
    paintArea->setShape(shape);
}
void MainWindow::ShowPenWidth(int value)//畫筆寬度
{
    QColor color=penColorFrame->palette().color(QPalette::Window);
    Qt::PenStyle style=Qt::PenStyle(penStyleComboBox->itemData(penStyleComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenCapStyle cap=Qt::PenCapStyle(penCapComboBox->itemData(penCapComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenJoinStyle join=Qt::PenJoinStyle(penJoinComboBox->itemData(penJoinComboBox->currentIndex(),Qt::UserRole).toInt());
 
 
    paintArea->setPen(QPen(color,value,style,cap,join));
}
void MainWindow::ShowPenColor()//畫筆顏色
{
    QColor color=QColorDialog::getColor(Qt::blue);
    penColorFrame->setPalette(QPalette(color));
    int value=penWidthSpinBox->value();
    Qt::PenStyle style=Qt::PenStyle(penStyleComboBox->itemData(penStyleComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenCapStyle cap=Qt::PenCapStyle(penCapComboBox->itemData(penCapComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenJoinStyle join=Qt::PenJoinStyle(penJoinComboBox->itemData(penJoinComboBox->currentIndex(),Qt::UserRole).toInt());
 
 
    paintArea->setPen(QPen(color,value,style,cap,join));
}
void MainWindow::ShowPenStyle(int styleValue)//畫筆風格
{
    QColor color=penColorFrame->palette().color(QPalette::Window);
    int value=penWidthSpinBox->value();
    Qt::PenStyle style=Qt::PenStyle(penStyleComboBox->itemData(styleValue,Qt::UserRole).toInt());
    Qt::PenCapStyle cap=Qt::PenCapStyle(penCapComboBox->itemData(penCapComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenJoinStyle join=Qt::PenJoinStyle(penJoinComboBox->itemData(penJoinComboBox->currentIndex(),Qt::UserRole).toInt());
 
 
    paintArea->setPen(QPen(color,value,style,cap,join));
}
void MainWindow::ShowPenCap(int capValue)//頂端風格
{
    QColor color=penColorFrame->palette().color(QPalette::Window);
    int value=penWidthSpinBox->value();
    Qt::PenStyle style=Qt::PenStyle(penStyleComboBox->itemData(penStyleComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenCapStyle cap=Qt::PenCapStyle(penCapComboBox->itemData(capValue,Qt::UserRole).toInt());
    Qt::PenJoinStyle join=Qt::PenJoinStyle(penJoinComboBox->itemData(penJoinComboBox->currentIndex(),Qt::UserRole).toInt());
 
 
    paintArea->setPen(QPen(color,value,style,cap,join));
}
void MainWindow::ShowPenJoin(int joinValue)//畫筆鏈接點風格
{
    QColor color=penColorFrame->palette().color(QPalette::Window);
    int value=penWidthSpinBox->value();
    Qt::PenStyle style=Qt::PenStyle(penStyleComboBox->itemData(penStyleComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenCapStyle cap=Qt::PenCapStyle(penCapComboBox->itemData(penCapComboBox->currentIndex(),Qt::UserRole).toInt());
    Qt::PenJoinStyle join=Qt::PenJoinStyle(penJoinComboBox->itemData(joinValue,Qt::UserRole).toInt());
 
 
    paintArea->setPen(QPen(color,value,style,cap,join));
}
void MainWindow::ShowFill()//填充模式
{
    Qt::FillRule fill=Qt::FillRule(fillComboBox->itemData(fillComboBox->currentIndex(),Qt::UserRole).toInt());
    paintArea->setFillRule(fill);
}
void MainWindow::ShowBrushColor()//畫刷顏色
{
    QColor color=QColorDialog::getColor(Qt::blue);
    brushColorFrame->setPalette(QPalette(color));
    ShowBrush(brushStyleComboBox->currentIndex());
}
void MainWindow::ShowBrush(int value)//畫刷風格
{
    QColor color=brushColorFrame->palette().color(QPalette::Window);//畫刷當前的顏色
    Qt::BrushStyle style=Qt::BrushStyle(brushStyleComboBox->itemData(value,Qt::UserRole).toInt());//獲取用戶選擇的畫刷風格
 
 
    if(style==Qt::LinearGradientPattern)//線性漸變
    {
        QLinearGradient linearGradient(0,0,400,400);//線性漸變的起止點位置
        linearGradient.setColorAt(0.0,Qt::white);//漸變比和漸變顏色
        linearGradient.setColorAt(0.2,color);
        linearGradient.setColorAt(1.0,Qt::black);
        paintArea->setBrush(linearGradient);
    }
    else if(style==Qt::RadialGradientPattern)//圓形漸變
    {
        QRadialGradient radialGradient(200,200,150,150,100);//中心點,半徑,焦點
        radialGradient.setColorAt(0.0,Qt::white);
        radialGradient.setColorAt(0.2,color);
        radialGradient.setColorAt(1.0,Qt::black);
        paintArea->setBrush(radialGradient);
    }
    else if(style==Qt::ConicalGradientPattern)//錐形漸變
    {
        QConicalGradient conicalGradient(200,200,30);
        conicalGradient.setColorAt(0.0,Qt::white);
        conicalGradient.setColorAt(0.2,color);
        conicalGradient.setColorAt(1.0,Qt::black);
        paintArea->setBrush(conicalGradient);
    }
    else if(style==Qt::TexturePattern)
    {
        paintArea->setBrush(QBrush(QPixmap("2.png")));
    }
    else
    {
        paintArea->setBrush(QBrush(color,style));
    }
 
 
}
MainWindow::~MainWindow()
{
 
 
}

 

 

簡述

Qt 中提供了強大的 2D 繪圖系統,可使用相同的 API 在屏幕和繪圖設備上進行繪製,它主要基於QPainter、QPaintDevice 和 QPaintEngine 這三個類。

  • QPainter 用於執行繪圖操做,其提供的 API 在 GUI 或 QImage、QOpenGLPaintDevice、QWidget 和QPaintDevice 顯示圖形(線、形狀、漸變等)、文本和圖像。
  • QPaintDevice 不直接繪製物理顯示畫面,而利用邏輯界面的中間媒介。例如,繪製矩形圖形時,爲了將對象繪製到 QWidget、QGLPixelBuffer、QImage、QPixmap、QPicture 等多種界面中間,必須使用 QPaintDevice。
  • QPaintEngine 提供了一些接口,可用於 QPainter 在不一樣的設備上進行繪製。

繪圖系統由 QPainter 完成具體的繪製操做,QPainter 類提供了大量高度優化的函數來完成 GUI 編程所須要的大部分繪製工做。它能夠繪製一切想要的圖形,從最簡單的一條直線到其餘任何複雜的圖形,例如:點、線、矩形、弧形、餅狀圖、多邊形、貝塞爾弧線等。此外,QPainter 也支持一些高級特性,例如反走樣(針對文字和圖形邊緣)、像素混合、漸變填充和矢量路徑等,QPainter 也支持線性變換,例如平移、旋轉、縮放。

QPainter 能夠在繼承自 QPaintDevice 類的任何對象上進行繪製操做。QPainter 也能夠與 QPrinter 一塊兒使用來打印文件和建立 PDF 文檔。這意味着一般能夠用相同的代碼在屏幕上顯示數據,也能夠生成打印形式的報告。

QPainter 通常在部件的繪圖事件 paintEvent() 中進行繪製,首先建立 QPainter 對象,而後進行圖形的繪製,最後記得銷燬 QPainter 對象。當窗口程序須要升級或者從新繪製時,調用此成員函數。使用 repaint()和 update() 後,調用函數 paintEvent()。

 

繪製文本

這裏寫圖片描述

void MainWindow::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); // 設置畫筆顏色 painter.setPen(QColor(0, 160, 230)); // 設置字體:微軟雅黑、點大小50、斜體 QFont font; font.setFamily("Microsoft YaHei"); font.setPointSize(50); font.setItalic(true); painter.setFont(font); // 繪製文本 painter.drawText(rect(), Qt::AlignCenter, "Qt"); }

 

首先爲該部件建立了一個 QPainter 對象,用於後面的繪製。使用 setPen() 來設置畫筆的顏色(淡藍色)。經過使用 QFont 來構建咱們想要的字體,setFamily()設置字體爲微軟雅黑、setPointSize() 設置點大小30、setItalic() 設置斜體, 而後經過 setFont() 來設置字體,最後調用 drawText() 來實現文本的繪製,這裏的 rect() 是指當前窗體的顯示區域,Qt::AlignCenter 指文本居中繪製。

繪製直線

這裏寫圖片描述

void MainWindow::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); // 反走樣 painter.setRenderHint(QPainter::Antialiasing, true); // 設置畫筆顏色 painter.setPen(QColor(0, 160, 230)); // 繪製直線 painter.drawLine(QPointF(0, height()), QPointF(width() / 2, height() / 2)); }

 

首先咱們經過 setRenderHint() 來設置反走樣,要麼繪製出來的線條會出現鋸齒,調用 setPen() 來設置畫筆顏色(淡藍色)。最後調用 drawLine() 來實現直線的繪製,其中 QPointF(0, height()) 是指直線的起點座標、QPointF(width() / 2, height() / 2) 是指直線的終點座標。

繪製矩形

這裏寫圖片描述

void MainWindow::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); // 反走樣 painter.setRenderHint(QPainter::Antialiasing, true); // 設置畫筆顏色、寬度 painter.setPen(QPen(QColor(0, 160, 230), 2)); // 設置畫刷顏色 painter.setBrush(QColor(255, 160, 90)); painter.drawRect(50, 50, 160, 100); }

 

首先咱們使用 setPen() 來設置畫筆顏色(淡藍色)、寬度(2 像素),用來設置矩形區域的邊框。而後使用setBrush() 來設置畫刷顏色(橙色),用來填充矩形區域,最後調用 drawRect() 來實現矩形的繪製,其中參數依次順序爲 x、y、w、h,是指區域從 x 爲 50,y 爲 50 的座標點起,寬度爲 160,高度爲 100 的矩形。

繪製弧線

這裏寫圖片描述

void MainWindow::paintEvent(QPaintEvent *event) { Q_UNUSED(event); // 矩形 QRectF rect(90.0, 90.0, 80.0, 90.0); // 起始角度 int startAngle = 30 * 16; // 跨越度數 int spanAngle = 120 * 16; QPainter painter(this); // 反走樣 painter.setRenderHint(QPainter::Antialiasing, true); // 設置畫筆顏色、寬度 painter.setPen(QPen(QColor(0, 160, 230), 2)); // 繪製弧線 painter.drawArc(rect, startAngle, spanAngle); }

 

畫弧線時,角度被分紅了十六分之一,就是說,若是要 30 度,就需是 30*16。它有起始角度和跨度,還有位置矩形,因此,要想畫出本身想要的弧線,就須要大概估算出各個參數的預估值。

繪製橢圓

這裏寫圖片描述

void MainWindow::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); // 反走樣 painter.setRenderHint(QPainter::Antialiasing, true); // 設置畫筆顏色、寬度 painter.setPen(QPen(QColor(0, 160, 230), 2)); // 繪製橢圓 painter.drawEllipse(QPointF(120, 60), 50, 20); // 設置畫刷顏色 painter.setBrush(QColor(255, 160, 90)); // 繪製圓 painter.drawEllipse(QPointF(120, 140), 40, 40); }

 

這裏咱們繪製了一個橢圓和一個圓形,都是調用 drawEllipse() 接口,咱們能夠很輕易的發現,若是爲橢圓的時候,後面兩個參數不同,圓形則相同。首先咱們來看第一個參數 QPointF 是指橢圓的中心點相對當前窗體 QPoint(0, 0) 點的位置,後面的參數指橢圓的 x 軸及 y 軸的半徑。

繪製多邊形

這裏寫圖片描述

void MainWindow::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); // 反走樣 painter.setRenderHint(QPainter::Antialiasing, true); // 設置畫筆顏色 painter.setPen(QColor(0, 160, 230)); // 各個點的座標 static const QPointF points[4] = {QPointF(30, 40), QPointF(60, 150), QPointF(150, 160), QPointF(220, 100)}; // 繪製多邊形 painter.drawPolygon(points, 4); }

 

首先,咱們定義一個個座標點的位置,這裏有四個點,分別爲:QPointF(30, 40)、QPointF(60, 150)、QPointF(150, 160)、 QPointF(220, 100),而後調用 drawPolygon() 將各個點鏈接起來,繪製爲多邊形。

繪製圖片

這裏寫圖片描述

void MainWindow::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); // 反走樣 painter.setRenderHint(QPainter::Antialiasing, true); // 繪製圖標 painter.drawPixmap(rect(), QPixmap(":/Images/logo")); }

經過 drawPixmap() 來繪製圖片,咱們能夠指定圖片繪製的區域 QRect,這裏爲整個界面的區域,當界面伸縮的時候,圖片也會跟着伸縮。

相關文章
相關標籤/搜索