QT5基础图形绘制

通过实例介绍画笔颜色,画笔线宽,画笔风格,画笔顶帽,画笔连接点,填充模式,铺展效果,画刷颜色和画笔风格设置的简单使用。
在这里插入图片描述
实现代码:
①绘图窗口类的实现:
头文件:

#ifndef PAINTAREA_H
#define PAINTAREA_H

#include <QWidget>
#include<QPen>
#include<QBrush>
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);
    void setShape(Shape);//设置形状
    void setPen(QPen);//设置画笔
    void setBrush(QBrush);//设置画刷
    void setFillRule(Qt::FillRule);//设置填充模式
    void paintEvent(QPaintEvent *);//画图事件
signals:
public slots:
private:
    Shape shape;
    QPen pen;
    QBrush brush;
    Qt::FillRule fillRule;
};

#endif // PAINTAREA_H

源文件的实现:

#include "paintarea.h"
#include<QPainter>
PaintArea::PaintArea(QWidget *parent) : QWidget(parent)
{
    this->setPalette(QPalette(Qt::white));//设置图形显示区域背景色
    this->setAutoFillBackground(true);//自动填充背景
    this->setMinimumSize(400,400);//设置最小尺寸
}
void PaintArea::setShape(Shape s)
{
    shape=s;
    this->update();
}
void PaintArea::setPen(QPen p)
{
    pen=p;
    this->update();
}
void PaintArea::setBrush(QBrush b)
{
    brush=b;
    this->update();
}
void PaintArea::setFillRule(Qt::FillRule rule)
{
    fillRule=rule;
    this->update();
}
void PaintArea::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setPen(pen);
    painter.setBrush(brush);
    QRect rect(50,100,300,200);//设置一个方形区域,为画图做准备
    //创建一个point数组,为画多边形做准备
    static 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,100);
    path.cubicTo(300,100,200,200,300,300);// c1  c2  endPoint  贝塞尔曲线
    path.cubicTo(100,300,200,200,100,100);
    switch (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,"Hello Qt");break;//文字
        case Pixmap:painter.drawPixmap(150,150,QPixmap("://butterfly.png"));break;//图片
        default:break;
    }
}

②主窗口的实现
头文件:

#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>
#include"paintarea.h"
#include<QLabel>
#include<QComboBox>
#include<QSpinBox>
#include<QPushButton>
#include<QGradient>
#include<QGridLayout>
#include<QHBoxLayout>
#include<QColorDialog>
#include<QPen>
#include<QDebug>
class MainWidget : public QWidget
{
    Q_OBJECT

public:
    MainWidget(QWidget *parent = 0);
    void createFunction();
    ~MainWidget();
protected slots:
    void showShape(int);
    void showPenColor();
    void showPenWidth(int);
    void showPenStyle(int);
    void showPenCap(int);
    void showPenJoin(int);
    void showFillRule();
    void showSpread();
    void showBrushColor();
    void showBrush(int);
private:
    //绘图区
    PaintArea *paintArea;
    //形状
    QLabel *shapeLabel;
    QComboBox *shapeBox;
    QLabel *penColorLabel;
    QFrame *penColorFrame;
    QPushButton *penColorButton;
    QLabel *penWidthLabel;
    QSpinBox *penWidthSpinBox;
    QLabel *penStyleLabel;
    QComboBox *penStyleComboBox;
    QLabel *penCapLabel;
    QComboBox *penCapComboBox;
    QLabel *penJoinLabel;
    QComboBox *penJoinComboBox;
    QLabel *fillRuleLabel;
    QComboBox *filleRuleComboBox;
    QLabel *spreadLabel;
    QComboBox *spreadComboBox;
    QGradient::Spread spread;
    QLabel *brushStyleLabel;
    QComboBox *brushStyleComboBox;
    QLabel *brushColorLabel;
    QFrame *brushColorFrame;
    QPushButton *brushColorButton;
    QGridLayout *rightLayout;
};

#endif // MAINWIDGET_H

源文件:

#include "mainwidget.h"

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
{
    paintArea =new PaintArea;
    this->createFunction();
}
void MainWidget::createFunction()
{
    //形状
    shapeLabel =new QLabel("图形形状:");
    shapeBox=new QComboBox;
    shapeBox->addItem("Line",PaintArea::Line);
    shapeBox->addItem("Rectangle",PaintArea::Rectangle);
    shapeBox->addItem("RoundRect",PaintArea::RoundRect);
    shapeBox->addItem("Ellipse",PaintArea::Ellipse);
    shapeBox->addItem("Polygon",PaintArea::Polygon);
    shapeBox->addItem("Polyline",PaintArea::Polyline);
    shapeBox->addItem("Points",PaintArea::Points);
    shapeBox->addItem("Arc",PaintArea::Arc);
    shapeBox->addItem("Path",PaintArea::Path);
    shapeBox->addItem("Text",PaintArea::Text);
    shapeBox->addItem("Pixmap",PaintArea::Pixmap);
    connect(shapeBox,SIGNAL(activated(int)),this,SLOT(showShape(int)));
    //画笔颜色
    penColorLabel=new QLabel("画笔颜色:");
    penColorFrame=new QFrame;
    penColorFrame->setFrameStyle(QFrame::Panel|QFrame::Sunken);//绘制面板以使内容显示为凸起或凹陷
    penColorFrame->setAutoFillBackground(true);
    penColorFrame->setPalette(Qt::blue);
    penColorButton=new QPushButton("更改");
    connect(penColorButton,SIGNAL(clicked()),this,SLOT(showPenColor()));
    //画笔线宽
    penWidthLabel=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("SolidLine",static_cast<int>(Qt::SolidLine));
    penStyleComboBox->addItem("DashLine",static_cast<int>(Qt::DashLine));
    penStyleComboBox->addItem("DotLine",static_cast<int>(Qt::DotLine));
    penStyleComboBox->addItem("DashDotLine",static_cast<int>(Qt::DashDotLine));
    penStyleComboBox->addItem("DashDotDotLine",static_cast<int>(Qt::DashDotDotLine));
    penStyleComboBox->addItem("CustomDashLine",static_cast<int>(Qt::CustomDashLine));
    connect(penStyleComboBox,SIGNAL(activated(int)),this,SLOT(showPenStyle(int)));
    //画笔顶帽
    penCapLabel=new QLabel("画笔顶帽:");
    penCapComboBox=new QComboBox;
    penCapComboBox->addItem("SquareCap",Qt::SquareCap);
    penCapComboBox->addItem("FlatCap",Qt::FlatCap);
    penCapComboBox->addItem("RoundCap",Qt::RoundCap);
    connect(penCapComboBox,SIGNAL(activated(int)),this,SLOT(showPenCap(int)));
    //画笔连接点
    penJoinLabel=new QLabel("画笔连接点:");
    penJoinComboBox=new QComboBox;
    penJoinComboBox->addItem("BevelJoin",Qt::BevelJoin);
    penJoinComboBox->addItem("MiterJoin",Qt::MiterJoin);
    penJoinComboBox->addItem("RoundJoin",Qt::RoundJoin);
    connect(penJoinComboBox,SIGNAL(activated(int)),this,SLOT(showPenJoin(int)));
    //填充模式
    fillRuleLabel=new QLabel("填充模式:");
    filleRuleComboBox=new QComboBox;
    filleRuleComboBox->addItem("Odd Even",Qt::OddEvenFill);
    filleRuleComboBox->addItem("Winding",Qt::WindingFill);
    connect(filleRuleComboBox,SIGNAL(activated(int)),this,SLOT(showFillRule()));
    //铺展效果
    spreadLabel=new QLabel("铺展效果:");
    spreadComboBox=new QComboBox;
    spreadComboBox->addItem("PadSpread",QGradient::PadSpread);
    spreadComboBox->addItem("RepeatSpread",QGradient::RepeatSpread);
    spreadComboBox->addItem("ReflectSpread",QGradient::ReflectSpread);
    connect(spreadComboBox,SIGNAL(activated(int)),this,SLOT(showSpread()));
    //画刷颜色
    brushColorLabel=new QLabel("画刷颜色:");
    brushColorFrame=new QFrame;
    brushColorFrame->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    brushColorFrame->setAutoFillBackground(true);
    brushColorFrame->setPalette(QPalette(Qt::green));
    brushColorButton=new QPushButton("更改");
    connect(brushColorButton,SIGNAL(clicked()),this,SLOT(showBrushColor()));
    //画刷风格
    brushStyleLabel=new QLabel("画刷风格:");
    brushStyleComboBox=new QComboBox;
    brushStyleComboBox->addItem("SolidPattern",static_cast<int>(Qt::SolidPattern));
    brushStyleComboBox->addItem("Dense1Pattern",static_cast<int>(Qt::Dense1Pattern));
    brushStyleComboBox->addItem("Dense2Pattern",static_cast<int>(Qt::Dense2Pattern));
    brushStyleComboBox->addItem("Dense3Pattern",static_cast<int>(Qt::Dense3Pattern));
    brushStyleComboBox->addItem("Dense4Pattern",static_cast<int>(Qt::Dense4Pattern));
    brushStyleComboBox->addItem("Dense5Pattern",static_cast<int>(Qt::Dense5Pattern));
    brushStyleComboBox->addItem("Dense6Pattern",static_cast<int>(Qt::Dense6Pattern));
    brushStyleComboBox->addItem("Dense7Pattern",static_cast<int>(Qt::Dense7Pattern));
    brushStyleComboBox->addItem("HorPattern",static_cast<int>(Qt::HorPattern));
    brushStyleComboBox->addItem("VerPattern",static_cast<int>(Qt::VerPattern));
    brushStyleComboBox->addItem("CrossPattern",static_cast<int>(Qt::CrossPattern));
    brushStyleComboBox->addItem("BDiagPattern",static_cast<int>(Qt::BDiagPattern));
    brushStyleComboBox->addItem("FDiagPattern)",static_cast<int>(Qt::FDiagPattern));
    brushStyleComboBox->addItem("DiagCrossPattern",static_cast<int>(Qt::DiagCrossPattern));
    brushStyleComboBox->addItem("LinearGradientPattern",static_cast<int>(Qt::LinearGradientPattern));
    brushStyleComboBox->addItem("ConicalGradientPattern",static_cast<int>(Qt::ConicalGradientPattern));
    brushStyleComboBox->addItem("RadialGradientPattern",static_cast<int>(Qt::RadialGradientPattern));
    brushStyleComboBox->addItem("TexturePattern",static_cast<int>(Qt::TexturePattern));
    connect(spreadComboBox,SIGNAL(activated(int)),this,SLOT(showBrush(int)));
    //右边布局
    rightLayout=new QGridLayout;
    rightLayout->addWidget(shapeLabel,0,0);
    rightLayout->addWidget(shapeBox,0,1);
    rightLayout->addWidget(penColorLabel,1,0);
    rightLayout->addWidget(penColorFrame,1,1);
    rightLayout->addWidget(penColorButton,1,2);
    rightLayout->addWidget(penWidthLabel,2,0);
    rightLayout->addWidget(penWidthSpinBox,2,1);
    rightLayout->addWidget(penStyleLabel,3,0);
    rightLayout->addWidget(penStyleComboBox,3,1);
    rightLayout->addWidget(penCapLabel,4,0);
    rightLayout->addWidget(penCapComboBox,4,1);
    rightLayout->addWidget(penJoinLabel,5,0);
    rightLayout->addWidget(penJoinComboBox,5,1);
    rightLayout->addWidget(fillRuleLabel,6,0);
    rightLayout->addWidget(filleRuleComboBox,6,1);
    rightLayout->addWidget(spreadLabel,7,0);
    rightLayout->addWidget(spreadComboBox,7,1);
    rightLayout->addWidget(brushColorLabel,8,0);
    rightLayout->addWidget(brushColorFrame,8,1);
    rightLayout->addWidget(brushColorButton,8,2);
    rightLayout->addWidget(brushStyleLabel,9,0);
    rightLayout->addWidget(brushStyleComboBox,9,1);
    QHBoxLayout *layout=new QHBoxLayout(this);
    layout->addWidget(paintArea);
    layout->addLayout(rightLayout);
    layout->setStretchFactor(paintArea,1);//设置要拉伸的小部件的拉伸因子
    layout->setStretchFactor(rightLayout,0);
    showShape(shapeBox->currentIndex());//显示默认图形
}
void MainWidget::showShape(int value)
{
    PaintArea::Shape shape=PaintArea::Shape(shapeBox->itemData(value,Qt::UserRole).toInt());
    paintArea->setShape(shape);
}
void MainWidget::showPenColor()
{
    QColor color=QColorDialog::getColor(static_cast<int>(Qt::red));
    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 penJoin=Qt::PenJoinStyle(penJoinComboBox->itemData(penJoinComboBox->currentIndex(),Qt::UserRole).toInt());//获取画笔连接点
    paintArea->setPen(QPen(color,value,style,cap,penJoin));
}
void MainWidget::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 penJoin=Qt::PenJoinStyle(penJoinComboBox->itemData(penJoinComboBox->currentIndex(),Qt::UserRole).toInt());//获取画笔连接点
    paintArea->setPen(QPen(color,value,style,cap,penJoin));
}
void MainWidget::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 penJoin=Qt::PenJoinStyle(penJoinComboBox->itemData(penJoinComboBox->currentIndex(),Qt::UserRole).toInt());//获取画笔连接点
    paintArea->setPen(QPen(color,value,style,cap,penJoin));
}
void MainWidget::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 penJoin=Qt::PenJoinStyle(penJoinComboBox->itemData(penJoinComboBox->currentIndex(),Qt::UserRole).toInt());//获取画笔连接点
    paintArea->setPen(QPen(color,value,style,cap,penJoin));
}
void MainWidget::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 penJoin=Qt::PenJoinStyle(penJoinComboBox->itemData(joinValue,Qt::UserRole).toInt());//获取画笔连接点
    paintArea->setPen(QPen(color,value,style,cap,penJoin));
}
void MainWidget::showFillRule()
{
    Qt::FillRule rule=Qt::FillRule(filleRuleComboBox->itemData(filleRuleComboBox->currentIndex(),Qt::UserRole).toInt());
    paintArea->setFillRule(rule);
}
void MainWidget::showSpread()
{
    spread=QGradient::Spread(spreadComboBox->itemData(spreadComboBox->currentIndex(),Qt::UserRole).toInt());
}
//与设置画笔颜色函数类似,但选定颜色后并不直接电泳paintArea对象的setBrush函数,而是调用showBrush函数设置显示区的画刷属性
void MainWidget::showBrushColor()
{
    qDebug()<<"1";
    QColor color=QColorDialog::getColor(static_cast<int>(Qt::red));
    brushColorFrame->setPalette(QPalette(color));
    this->showBrush(brushStyleComboBox->currentIndex());
}
void MainWidget::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 linear(0,0,400,400);
        linear.setColorAt(0.0,Qt::white);
        linear.setColorAt(0.2,color);
        linear.setColorAt(1.0,Qt::black);
        linear.setSpread(spread);
        paintArea->setBrush(linear);
    }
    else if(style==Qt::RadialGradientPattern)
    {
        QRadialGradient radial(200,200,150,150,100);
        radial.setColorAt(0.0,Qt::white);
        radial.setColorAt(0.2,color);
        radial.setColorAt(1.0,Qt::black);
        radial.setSpread(spread);
        paintArea->setBrush(radial);
    }
    else if(style==Qt::ConicalGradientPattern)
    {
        QConicalGradient conical(200,200,30);
        conical.setColorAt(0.0,Qt::white);
        conical.setColorAt(0.2,color);
        conical.setColorAt(1.0,Qt::black);
        paintArea->setBrush(conical);
    }
    else if (style==Qt::TexturePattern)
    {
        paintArea->setBrush(QBrush(QPixmap("://butterfly.png")));
    }
    else
    {
        paintArea->setBrush(QBrush(color,style));
    }
}
MainWidget::~MainWidget()
{

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值