QT图形绘制

QT图形绘制

程序

画图

.h
#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
.cpp
#include "paintarea.h"
#include <QPainter>

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

void PaintArea::setShape(PaintArea::Shape s)
{
    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(50,100,300,200);//设定一个方形区域
    static const QPoint points[4]=
    {
        QPoint(150,100),
        QPoint(300,150),
        QPoint(350,250),
        QPoint(100,300),
    };//定义一个QPoint的数组包含四个点
    int startAngle=30*16;//起始角
    int spanAngle=120*16;//跨度角
    QPainterPath path;//新建一个QPainterPath对象为画路径做准备
    path.addRect(150,150,100,100);
    path.moveTo(100,100);
    path.cubicTo(300,100,200,200,300,300);
    path.cubicTo(100,300,200,200,100,100);
    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);break;
        case Polyline://多边线
            p.drawPolygon(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("butterfly.png"));break;
        default:break;
    }
}

界面

.h
#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>
#include <QLabel>
#include <QComboBox>
#include <QSpinBox>
#include <QPushButton>
#include <QGridLayout>
#include <QGradient>
#include "paintarea.h"

class MainWidget : public QWidget
{
    Q_OBJECT

public:
    MainWidget(QWidget *parent = 0);
    ~MainWidget();
private:
    PaintArea *paintArea;
    QLabel *shapeLabel;
    QComboBox *shapeComboBox;
    QLabel *penWidthLabel;
    QSpinBox *penWidthSpinBox;
    QLabel *penColorLabel;
    QFrame *penColorFrame;
    QPushButton *penColorBtn;
    QLabel *penStyleLabel;
    QComboBox *penStyleComboBox;
    QLabel *penCapLabel;
    QComboBox *penCapComboBox;
    QLabel *penJoinLabel;
    QComboBox *penJoinComboBox;
    QLabel *fillRuleLabel;
    QComboBox *fillRuleComboBox;
    QLabel *spreadLabel;
    QComboBox *spreadComboBox;
    QGradient::Spread spread;
    QLabel *brushStyleLabel;
    QComboBox* brushStyleComboBox;
    QLabel *brushColorLabel;
    QFrame* brushColorFrame;
    QPushButton *brushColorBtn;
    QGridLayout *rightLayout;
protected slots:
    void ShowShape(int);
    void ShowPenWidth(int);
    void ShowPenColor();
    void ShowPenStyle(int);
    void ShowPenCap(int);
    void ShowPenJoin(int);
    void ShowSpreadStyle();
    void ShowFillRule();
    void ShowBrushColor();
    void ShowBrush(int);
};

#endif // MAINWIDGET_H

.cpp
#include "mainwidget.h"
#include <QColorDialog>

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
{
    paintArea=new PaintArea;
    shapeLabel=new QLabel("形状:");
    shapeComboBox=new QComboBox;
    shapeComboBox->addItem(tr("Line"),PaintArea::Line);
    shapeComboBox->addItem("Rectangle",PaintArea::Rectangle);
    shapeComboBox->addItem("RoundRect",PaintArea::RoundRect);
    shapeComboBox->addItem("Ellipse",PaintArea::Ellipse);
    shapeComboBox->addItem("Polygon",PaintArea::Polygon);
    shapeComboBox->addItem("Polyline",PaintArea::Polyline);
    shapeComboBox->addItem("Points",PaintArea::Points);
    shapeComboBox->addItem("Arc",PaintArea::Arc);
    shapeComboBox->addItem("Path",PaintArea::Path);
    shapeComboBox->addItem("Text",PaintArea::Text);
    shapeComboBox->addItem("Pixmap",PaintArea::Pixmap);
    connect(shapeComboBox,SIGNAL(activated(int)),this,SLOT(ShowShape(int)));
    penColorLabel=new QLabel("画笔颜色:");
    penColorFrame=new QFrame;
    penColorFrame->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    penColorFrame->setAutoFillBackground(true);
    penColorFrame->setPalette(QPalette(Qt::blue));
    penColorBtn=new QPushButton("更改:");
    connect(penColorBtn,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("填充规则:");
    fillRuleComboBox=new QComboBox;
    fillRuleComboBox->addItem("Odd Even",Qt::OddEvenFill);
    fillRuleComboBox->addItem("Winding",Qt::WindingFill);
    connect(fillRuleComboBox,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(ShowSpreadStyle()));
    brushColorLabel=new QLabel("画刷颜色:");
    brushColorFrame=new QFrame;
    brushColorFrame->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    brushColorFrame->setAutoFillBackground(true);
    brushColorFrame->setPalette(QPalette(Qt::green));
    brushColorBtn=new QPushButton("更改:");
    connect(brushColorBtn,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(brushStyleComboBox,SIGNAL(activated(int)),this,SLOT(ShowBrush(int)));
    rightLayout=new QGridLayout;
    rightLayout->addWidget(shapeLabel,0,0);
    rightLayout->addWidget(shapeComboBox,0,1);
    rightLayout->addWidget(penColorLabel,1,0);
    rightLayout->addWidget(penColorFrame,1,1);
    rightLayout->addWidget(penColorBtn,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(fillRuleComboBox,6,1);
    rightLayout->addWidget(spreadLabel,7,0);
    rightLayout->addWidget(spreadComboBox,7,1);
    rightLayout->addWidget(brushColorLabel,8,0);
    rightLayout->addWidget(brushColorFrame,8,1);
    rightLayout->addWidget(brushColorBtn,8,2);
    rightLayout->addWidget(brushStyleLabel,9,0);
    rightLayout->addWidget(brushStyleComboBox,9,1);
    QHBoxLayout *mainLayout=new QHBoxLayout(this);
    mainLayout->addWidget(paintArea);
    mainLayout->addLayout(rightLayout);
    mainLayout->setStretchFactor(paintArea,1);
    mainLayout->setStretchFactor(rightLayout,0);
    ShowShape(shapeComboBox->currentIndex());
}

MainWidget::~MainWidget()
{

}

void MainWidget::ShowShape(int value)
{
    PaintArea::Shape shape=PaintArea::Shape(shapeComboBox->itemData(value,Qt::UserRole).toInt());//获得shapeComboBox返回的当前下拉列表数据,toInt()获得该shape在枚举类型中的序号
    paintArea->setShape(shape);
}

void MainWidget::ShowPenWidth(int)
{
    QColor color = penColorFrame->palette().color(QPalette::Window);
    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 MainWidget::ShowPenColor()
{
    QColor color = QColorDialog::getColor(static_cast<int>(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 MainWidget::ShowPenStyle(int)
{
    QColor color = penColorFrame->palette().color(QPalette::Window);
    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 MainWidget::ShowPenCap(int)
{
    QColor color = penColorFrame->palette().color(QPalette::Window);
    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 MainWidget::ShowPenJoin(int)
{
    QColor color = penColorFrame->palette().color(QPalette::Window);
    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 MainWidget::ShowSpreadStyle()
{
    spread=QGradient::Spread(spreadComboBox->itemData(spreadComboBox->currentIndex(),Qt::UserRole).toInt());
}

void MainWidget::ShowFillRule()
{
    Qt::FillRule rule= Qt::FillRule(fillRuleComboBox->itemData(fillRuleComboBox->currentIndex(),Qt::UserRole).toInt());
    paintArea->setFillRule(rule);
}

void MainWidget::ShowBrushColor()
{
    QColor color = QColorDialog::getColor(static_cast<int>(Qt::blue));
    brushColorFrame->setPalette(QPalette(color));
    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 linearGradient(0,0,400,400);
        linearGradient.setColorAt(0.0,Qt::white);
        linearGradient.setColorAt(0.2,color);
        linearGradient.setColorAt(1.0,Qt::black);
        linearGradient.setSpread(spread);
        paintArea->setBrush(linearGradient);
    }
    else if(style==Qt::RadialGradientPattern)
    {
        QRadialGradient radialGradient(200,200,150,150,150);
        radialGradient.setColorAt(0.0,Qt::white);
        radialGradient.setColorAt(0.2,color);
        radialGradient.setColorAt(1.0,Qt::black);
        radialGradient.setSpread(spread);
        paintArea->setBrush(radialGradient);
    }
    else if(style==Qt::ConicalGradientPattern)
    {
        QConicalGradient conicalGradient(200,2000,30);
        conicalGradient.setColorAt(0.0,Qt::white);
        conicalGradient.setColorAt(0.2,color);
        conicalGradient.setColorAt(1.0,Qt::black);
        conicalGradient.setSpread(spread);
        paintArea->setBrush(conicalGradient);
    }
    else if(style==Qt::TexturePattern)
    {
        paintArea->setBrush(QBrush(QPixmap("butterfly.png")));
    }
    else
    {
        paintArea->setBrush(QBrush(color,style));
    }
}

效果展示

在这里插入图片描述

发布了31 篇原创文章 · 获赞 3 · 访问量 286

猜你喜欢

转载自blog.csdn.net/weixin_44011306/article/details/105449865
今日推荐