Qt图形绘制QPainter

1>基本图形

在这里插入图片描述注意:用QPainter画弧度所使用的角度值,是以1/16°为单位的,在画弧度的时候1°用16表示。
另QPainter::drawPixmap()可直接将图片画到部件上。
兼容模式QPainter::CompositionMode

2>QPainterPath绘制简单图型

QPainterPath为QPainter类提供了一个存储容器,里面包含了所要绘制的内容的集合及绘制顺序,如长方形、多边形、曲线等各种任意图形。当需要绘制此预先存储在QPainterPath对象中的内容时,只需调用QPainter类的DrawPath()即可,如addRect()加入一个方形,addEllipse()加入一个椭圆形,addText()加入文本。

3>画笔风格

在这里插入图片描述在这里插入图片描述
在这里插入图片描述Qt::OddEvenFill填充规则:从图形中某一点画一条水平线到图形外,若这条线与图形边线的交点为奇数则说明此点位于图形的内部;若交点为偶数,则此点在图形的外部。
Qt::WindingFill填充规则:从图形总某一点画一条水平线到图形外,每个交点外边线方向可能向上、向下、方向相反的相互抵消,若结果不为0表此点在图形内,若为0则在图形外。其中边线的方向是由QPainterPath创建时根据描述的顺序决定的,如果采用addRect(),或addPolygon()等函数加入的图形默认为顺时针方向。

在这里插入图片描述在这里插入图片描述

2>代码

paintArea.h

#ifndef PAINTAREA_H
#define PAINTAREA_H

#include <QWidget>
#include<QPen>

class paintArea : public QWidget
{
    Q_OBJECT
public:
    enum Shape{Line,Rectangle,RoundRect,Ellipse,Polygon,
              Polyline,Points,Arc,Path,Text,Pixmap};
    explicit paintArea(QWidget *parent = nullptr);

public:
    void setShape(Shape shape);
    void setPen(QPen pen);
    void setBrush(QBrush brush);
    void setFillRule(Qt::FillRule rule);

protected:
    void paintEvent(QPaintEvent *event) override;

signals:

public slots:

public:
     QPen m_pen;
private:
    Shape m_shape;

    QBrush m_brush;
    Qt::FillRule m_fillRule;
};




#endif // PAINTAREA_H

paintArea.cpp

#include "paintarea.h"
#include<QPaintEvent>
#include<QPainter>

paintArea::paintArea(QWidget *parent) : QWidget(parent)
{
    setPalette(QPalette(Qt::white));
    setAutoFillBackground(true);
    setFixedSize(400,400);
}

void paintArea::setShape(paintArea::Shape shape)
{
    m_shape = shape;
    update();
}

void paintArea::setPen(QPen pen)
{
    m_pen = pen;
    update();
}

void paintArea::setBrush(QBrush brush)
{
    m_brush = brush;
    update();
}

void paintArea::setFillRule(Qt::FillRule rule)
{
    m_fillRule = rule;
    update();
}

void paintArea::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setPen(m_pen);
    painter.setBrush(m_brush);

    QRect rect(50,100,300,200);

    const QPoint points[4] = {
        QPoint(150,100),
        QPoint(300,150),
        QPoint(350,250),
        QPoint(100,300)
    };

    int startAngle = 30 * 16;
    int spanAngle = 120 * 16;

    QPainterPath path;
    path.addRect(150,150,100,100);
    path.moveTo(100,200);
    path.cubicTo(300,100,200,200,300,300);
    path.cubicTo(100,300,200,200,100,100);
    path.setFillRule(m_fillRule);

    switch (m_shape) {
    case Line:
        painter.drawLine(rect.topLeft(),rect.bottomRight());
        break;
    case Rectangle:
        painter.drawRect(rect);
        break;
    case RoundRect:
        painter.drawRoundRect(rect);
        break;
    case Ellipse:
        painter.drawEllipse(rect);
        break;
    case Polygon:
        painter.drawPolygon(points,4);
            break;
    case Polyline:
        painter.drawPolyline(points,4);
            break;
    case Points:
        painter.drawPoints(points,4);
        break;
    case Arc:
        painter.drawArc(rect,startAngle,spanAngle);
        break;
    case Path:
        painter.drawPath(path);
        break;
    case Text:
        painter.drawText(rect,Qt::AlignCenter,tr("Hello World"));
        break;
    case Pixmap:
        painter.drawPixmap(50,100,QPixmap("targe.png"));
        break;
    default:
        break;
    }
}

textParinter.h

#ifndef TEXTPARINTER_H
#define TEXTPARINTER_H

#include <QDialog>
#include"paintarea.h"
#include<QLabel>
#include<QPushButton>
#include<QComboBox>
#include<QSpinBox>
#include<QFrame>
#include<QGridLayout>

class textParinter : public QDialog
{
    Q_OBJECT

public:
    textParinter(QWidget *parent = 0);
    ~textParinter();
    void initWidget();

public slots:
    ShowShape(int value);
    ShowPen();
    ShowPenWidget(int value);
    ShowPenStyle(int value);
    ShowPenCap(int value);
    ShowPenJoin(int value);
    ShowFillRule(int value);
    ShowSpreadStyle(int value);
    ShowBrushColor();
    ShowBrush(int value);

private:
    paintArea *m_paintArea;//绘图区域
    QLabel *m_shapeLabel;//形状
    QComboBox *m_shapeComboBox;
    QLabel *m_penColorLabel;//颜色
    QFrame *m_penColorFrame;
    QPushButton *m_penColorBtn;
    QLabel *m_penWidthLabel;//线宽
    QSpinBox *m_penWidthSpinBox;
    QLabel *m_penStyleLabel;//画笔风格
    QComboBox *m_penStyleComboBox;
    QLabel *m_penCapLabel;//笔帽风格
    QComboBox *m_penCapComboBox;
    QLabel *m_penJoinLabel;//画笔连接点
    QComboBox *m_penJoinComboBox;
    QLabel *m_fillRuleLabel;//填充模式
    QComboBox *m_fillRuleComboBox;
    QLabel *m_spreadLabel;//铺展效果
    QComboBox *m_spreadComboBox;
    QGradient::Spread m_spread;
    QLabel *m_brushColorLabel;//画刷颜色
    QFrame *m_brushColorFrame;
    QPushButton *m_brushColorBtn;
    QLabel *m_brushStyleLabel;//画刷风格
    QComboBox *m_brushStyleComboBox;
    QGridLayout *m_rightLayout;
};

#endif // TEXTPARINTER_H

textParinter.cpp

#include "textparinter.h"
#include"paintarea.h"
#include<QColorDialog>

textParinter::textParinter(QWidget *parent)
    : QDialog(parent)
{
    initWidget();
}

textParinter::~textParinter()
{

}

void textParinter::initWidget()
{
    m_paintArea = new paintArea;
    m_shapeLabel = new QLabel(tr("形状"));
    m_shapeComboBox = new QComboBox;

    m_shapeComboBox->addItem(tr("line"),paintArea::Line);
    m_shapeComboBox->addItem(tr("Rectangle"),paintArea::Rectangle);
    m_shapeComboBox->addItem(tr("RoundedRect"),paintArea::RoundRect);
    m_shapeComboBox->addItem(tr("Ellipse"),paintArea::Ellipse);
    m_shapeComboBox->addItem(tr("Polygon"),paintArea::Polygon);
    m_shapeComboBox->addItem(tr("Polyline"),paintArea::Polyline);
    m_shapeComboBox->addItem(tr("Points"),paintArea::Points);
    m_shapeComboBox->addItem(tr("Arc"),paintArea::Arc);
    m_shapeComboBox->addItem(tr("Path"),paintArea::Path);
    m_shapeComboBox->addItem(tr("Text"),paintArea::Text);
    m_shapeComboBox->addItem(tr("Pixmap"),paintArea::Pixmap);
    connect(m_shapeComboBox,SIGNAL(activated(int)),this,SLOT(ShowShape(int)));

    m_penColorLabel = new QLabel(tr("画笔颜色"));
    m_penColorFrame = new QFrame;
    m_penColorFrame->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    m_penColorFrame->setAutoFillBackground(true);
    m_penColorFrame->setPalette(QPalette(Qt::black));
    m_penColorBtn = new QPushButton(tr("更改"));
    connect(m_penColorBtn,SIGNAL(clicked()),this,SLOT(ShowPen()));

    m_penWidthLabel = new QLabel(tr("画线宽度"));
    m_penWidthSpinBox = new QSpinBox;
    m_penWidthSpinBox->setRange(1,20);
    connect(m_penWidthSpinBox,SIGNAL(valueChanged(int)),this,SLOT(ShowPenWidget(int)));

    m_penStyleLabel = new QLabel(tr("画笔风格"));
    m_penStyleComboBox = new QComboBox;
    m_penStyleComboBox->addItem(tr("SolidLine"),static_cast<int>(Qt::SolidLine));
    m_penStyleComboBox->addItem(tr("DashLine"),static_cast<int>(Qt::DashLine));
    m_penStyleComboBox->addItem(tr("DotLine"),static_cast<int>(Qt::DotLine));
    m_penStyleComboBox->addItem(tr("DashDotLine"),static_cast<int>(Qt::DashDotLine));
    m_penStyleComboBox->addItem(tr("DashDotDotLine"),static_cast<int>(Qt::DashDotDotLine));
    m_penStyleComboBox->addItem(tr("CustomDashLine"),static_cast<int>(Qt::CustomDashLine));
    connect(m_penStyleComboBox,SIGNAL(activated(int)),this,SLOT(ShowPenStyle(int)));

    m_penCapLabel = new QLabel(tr("画笔笔帽"));
    m_penCapComboBox = new QComboBox;
    m_penCapComboBox->addItem(tr("SquareCap"),Qt::SquareCap);
    m_penCapComboBox->addItem(tr("FlatCap"),Qt::FlatCap);
    m_penCapComboBox->addItem(tr("RoundCap"),Qt::RoundCap);
    connect(m_penCapComboBox,SIGNAL(activated(int)),this,SLOT(ShowPenCap(int)));

    m_penJoinLabel = new QLabel(tr("画笔连接点"));
    m_penJoinComboBox = new QComboBox;
    m_penJoinComboBox->addItem(tr("BevelJoin"),Qt::BevelJoin);
    m_penJoinComboBox->addItem(tr("MiterJoin"),Qt::MiterJoin);
    m_penJoinComboBox->addItem(tr("RoundJoin"),Qt::RoundJoin);
    connect(m_penJoinComboBox,SIGNAL(activated(int)),this,SLOT(ShowPenJoin(int)));

    m_fillRuleLabel = new QLabel(tr("填充模式"));
    m_fillRuleComboBox = new QComboBox;
    m_fillRuleComboBox->addItem(tr("Odd Even"),Qt::OddEvenFill);
    m_fillRuleComboBox->addItem(tr("Winding"),Qt::WindingFill);
    connect(m_fillRuleComboBox,SIGNAL(activated(int)),this,SLOT(ShowFillRule(int)));

    m_spreadLabel = new QLabel(tr("铺展效果"));
    m_spreadComboBox = new QComboBox;
    m_spreadComboBox->addItem(tr("PadStread"),QGradient::PadSpread);
    m_spreadComboBox->addItem(tr("RepeatSpread"),QGradient::RepeatSpread);
    m_spreadComboBox->addItem(tr("ReflectSpread"),QGradient::ReflectSpread);
    connect(m_spreadComboBox,SIGNAL(activated(int)),this,SLOT(ShowSpreadStyle(int)));

    m_brushColorLabel = new QLabel(tr("画刷颜色"));
    m_brushColorFrame = new QFrame;
    m_brushColorFrame->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    m_brushColorFrame->setAutoFillBackground(true);
    m_brushColorFrame->setPalette(QPalette(Qt::red));
    m_brushColorBtn = new QPushButton(tr("更改"));
    connect(m_brushColorBtn,SIGNAL(clicked()),this,SLOT(ShowBrushColor()));

    m_brushStyleLabel = new QLabel(tr("画刷风格"));
    m_brushStyleComboBox = new QComboBox;
    m_brushStyleComboBox->addItem(tr("SolidPattern"),static_cast<int>(Qt::SolidPattern));
    m_brushStyleComboBox->addItem(tr("Dense1Pattern"),static_cast<int>(Qt::Dense1Pattern));
    m_brushStyleComboBox->addItem(tr("Dense2Pattern"),static_cast<int>(Qt::Dense2Pattern));
    m_brushStyleComboBox->addItem(tr("Dense3Pattern"),static_cast<int>(Qt::Dense3Pattern));
    m_brushStyleComboBox->addItem(tr("Dense4Pattern"),static_cast<int>(Qt::Dense4Pattern));
    m_brushStyleComboBox->addItem(tr("Dense5Pattern"),static_cast<int>(Qt::Dense5Pattern));
    m_brushStyleComboBox->addItem(tr("Dense6Pattern"),static_cast<int>(Qt::Dense6Pattern));
    m_brushStyleComboBox->addItem(tr("Dense7Pattern"),static_cast<int>(Qt::Dense7Pattern));
    m_brushStyleComboBox->addItem(tr("HorPattern"),static_cast<int>(Qt::HorPattern));
    m_brushStyleComboBox->addItem(tr("VerPattern"),static_cast<int>(Qt::VerPattern));
    m_brushStyleComboBox->addItem(tr("CrossPattern"),static_cast<int>(Qt::CrossPattern));
    m_brushStyleComboBox->addItem(tr("BDiagPattern"),static_cast<int>(Qt::BDiagPattern));
    m_brushStyleComboBox->addItem(tr("FDiagPattern"),static_cast<int>(Qt::FDiagPattern));
    m_brushStyleComboBox->addItem(tr("DiagCrossPattern"),static_cast<int>(Qt::DiagCrossPattern));
    m_brushStyleComboBox->addItem(tr("LinearGradientPattern"),static_cast<int>(Qt::LinearGradientPattern));
    m_brushStyleComboBox->addItem(tr("ConicalGradientPattern"),static_cast<int>(Qt::ConicalGradientPattern));
    m_brushStyleComboBox->addItem(tr("RadialGradientPattern"),static_cast<int>(Qt::RadialGradientPattern));
    m_brushStyleComboBox->addItem(tr("TexturePattern"),static_cast<int>(Qt::TexturePattern));
    connect(m_brushStyleComboBox,SIGNAL(activated(int)),this,SLOT(ShowBrush(int)));

    //右侧布局
    m_rightLayout = new QGridLayout;
    m_rightLayout->addWidget(m_shapeLabel,0,0);
    m_rightLayout->addWidget(m_shapeComboBox,0,1);
    m_rightLayout->addWidget(m_penColorLabel,1,0);
    m_rightLayout->addWidget(m_penColorFrame,1,1);
    m_rightLayout->addWidget(m_penColorBtn,1,2);
    m_rightLayout->addWidget(m_penWidthLabel,2,0);
    m_rightLayout->addWidget(m_penWidthSpinBox,2,1);
    m_rightLayout->addWidget(m_penStyleLabel,3,0);
    m_rightLayout->addWidget(m_penStyleComboBox,3,1);
    m_rightLayout->addWidget(m_penCapLabel,4,0);
    m_rightLayout->addWidget(m_penCapComboBox,4,1);
    m_rightLayout->addWidget(m_penJoinLabel,5,0);
    m_rightLayout->addWidget(m_penJoinComboBox,5,1);
    m_rightLayout->addWidget(m_fillRuleLabel,6,0);
    m_rightLayout->addWidget(m_fillRuleComboBox,6,1);
    m_rightLayout->addWidget(m_spreadLabel,7,0);
    m_rightLayout->addWidget(m_spreadComboBox,7,1);
    m_rightLayout->addWidget(m_brushColorLabel,8,0);
    m_rightLayout->addWidget(m_brushColorFrame,8,1);
    m_rightLayout->addWidget(m_brushColorBtn,8,2);
    m_rightLayout->addWidget(m_brushStyleLabel,9,0);
    m_rightLayout->addWidget(m_brushStyleComboBox,9,1);

    QHBoxLayout *boxLayout = new QHBoxLayout(this);
    boxLayout->addWidget(m_paintArea);
    boxLayout->addLayout(m_rightLayout);

    ShowShape(m_shapeComboBox->currentIndex());
}

textParinter::ShowShape(int value)
{
    paintArea::Shape shape = paintArea::Shape(m_shapeComboBox->itemData(value).toInt());
    m_paintArea->setShape(shape);
}

textParinter::ShowPen()
{
     QColor color = QColorDialog::getColor(Qt::black);
     m_penColorFrame->setPalette(QPalette(color));
     int value = m_penWidthSpinBox->value();

     int styleIndex = m_penStyleComboBox->currentIndex();
     Qt::PenStyle style = Qt::PenStyle(m_penStyleComboBox->itemData(styleIndex).toInt());

     int capIndex = m_penCapComboBox->currentIndex();
     Qt::PenCapStyle cap = Qt::PenCapStyle(m_penCapComboBox->itemData(capIndex).toInt());

     int joinIndex = m_penJoinComboBox->currentIndex();
     Qt::PenJoinStyle join = Qt::PenJoinStyle(m_penJoinComboBox->itemData(joinIndex).toInt());

     m_paintArea->setPen(QPen(color,value,style,cap,join));
}

textParinter::ShowPenWidget(int value)
{
    QColor color = m_penColorFrame->palette().color(QPalette::Window);
    int penWidget = value;

    int styleIndex = m_penStyleComboBox->currentIndex();
    Qt::PenStyle style = Qt::PenStyle(m_penStyleComboBox->itemData(styleIndex).toInt());

    int capIndex = m_penCapComboBox->currentIndex();
    Qt::PenCapStyle cap = Qt::PenCapStyle(m_penCapComboBox->itemData(capIndex).toInt());

    int joinIndex = m_penJoinComboBox->currentIndex();
    Qt::PenJoinStyle join = Qt::PenJoinStyle(m_penJoinComboBox->itemData(joinIndex).toInt());

    m_paintArea->setPen(QPen(color,penWidget,style,cap,join));
}

textParinter::ShowPenStyle(int value)
{
    QColor color = m_penColorFrame->palette().color(QPalette::Window);
    int penWidget = m_penWidthSpinBox->value();

    Qt::PenStyle style = Qt::PenStyle(m_penStyleComboBox->itemData(value).toInt());

    //自定义绘图
    if(style == Qt::CustomDashLine){
        QVector<qreal> dashes;
        qreal space = 4;
        dashes << 1 << space << 2 << space << 3 << space << 4 << space;
        m_paintArea->m_pen.setDashPattern(dashes);
        m_paintArea->update();
    }else{

        int capIndex = m_penCapComboBox->currentIndex();
        Qt::PenCapStyle cap = Qt::PenCapStyle(m_penCapComboBox->itemData(capIndex).toInt());

        int joinIndex = m_penJoinComboBox->currentIndex();
        Qt::PenJoinStyle join = Qt::PenJoinStyle(m_penJoinComboBox->itemData(joinIndex).toInt());

        m_paintArea->setPen(QPen(color,penWidget,style,cap,join));
    }
}

textParinter::ShowPenCap(int value)
{
    QColor color = m_penColorFrame->palette().color(QPalette::Window);
    int penWidget = m_penWidthSpinBox->value();

    int styleIndex = m_penStyleComboBox->currentIndex();
    Qt::PenStyle style = Qt::PenStyle(m_penStyleComboBox->itemData(styleIndex).toInt());

    Qt::PenCapStyle cap = Qt::PenCapStyle(m_penCapComboBox->itemData(value).toInt());

    int joinIndex = m_penJoinComboBox->currentIndex();
    Qt::PenJoinStyle join = Qt::PenJoinStyle(m_penJoinComboBox->itemData(joinIndex).toInt());

    m_paintArea->setPen(QPen(color,penWidget,style,cap,join));
}

textParinter::ShowPenJoin(int value)
{
    QColor color = m_penColorFrame->palette().color(QPalette::Window);
    int penWidget = m_penWidthSpinBox->value();

    int styleIndex = m_penStyleComboBox->currentIndex();
    Qt::PenStyle style = Qt::PenStyle(m_penStyleComboBox->itemData(styleIndex).toInt());

    int capIndex = m_penCapComboBox->currentIndex();
    Qt::PenCapStyle cap = Qt::PenCapStyle(m_penCapComboBox->itemData(capIndex).toInt());

    Qt::PenJoinStyle join = Qt::PenJoinStyle(m_penJoinComboBox->itemData(value).toInt());

    m_paintArea->setPen(QPen(color,penWidget,style,cap,join));
}

textParinter::ShowFillRule(int value)
{
    Qt::FillRule rule = Qt::FillRule(value);
    m_paintArea->setFillRule(rule);
}

textParinter::ShowSpreadStyle(int value)
{
    m_spread = QGradient::Spread(value);
}

textParinter::ShowBrushColor()
{
    QColor color = QColorDialog::getColor(Qt::green);
    m_brushColorFrame->setPalette(QPalette(color));
    ShowBrush(m_brushStyleComboBox->currentIndex());
}

textParinter::ShowBrush(int value)
{
    QColor color = m_brushColorFrame->palette().color(QPalette::Window);

    int brush = m_brushStyleComboBox->itemData(value).toInt();
    Qt::BrushStyle style = Qt::BrushStyle(brush);

    if(style == Qt::LinearGradientPattern){
        //指定线性渐变的区域
        QLinearGradient lineGradient(0,0,400,400);

        //指定某个位置的渐变色,pos取值范围0~1
        lineGradient.setColorAt(0.0,Qt::white);
        lineGradient.setColorAt(0.2,color);
        lineGradient.setColorAt(1,Qt::black);
        lineGradient.setSpread(m_spread);

        m_paintArea->setBrush(lineGradient);

    }//环形渲染
    else if(style == Qt::RadialGradientPattern){

        //中心坐标,半径长度,焦点坐标
        //如果需要对称则中心坐标和焦点坐标保持一致
        QRadialGradient radialGradient(200,200,150,200,200);
        radialGradient.setColorAt(0.0,Qt::white);
        radialGradient.setColorAt(0.2,color);
        radialGradient.setColorAt(1,Qt::black);

        m_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,Qt::black);

        m_paintArea->setBrush(conicalGradient);
    }
    else if(style == Qt::TexturePattern){

        m_paintArea->setBrush(QBrush(QPixmap("targe.png")));
    }
    else{

        m_paintArea->setBrush(QBrush(color,style));
    }

}

在这里插入图片描述在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值