注意:用QPainter画弧度所使用的角度值,是以1/16°为单位的,在画弧度的时候1°用16表示。
另QPainter::drawPixmap()可直接将图片画到部件上。
兼容模式QPainter::CompositionMode
QPainterPath为QPainter类提供了一个存储容器,里面包含了所要绘制的内容的集合及绘制顺序,如长方形、多边形、曲线等各种任意图形。当需要绘制此预先存储在QPainterPath对象中的内容时,只需调用QPainter类的DrawPath()即可,如addRect()加入一个方形,addEllipse()加入一个椭圆形,addText()加入文本。
Qt::OddEvenFill填充规则:从图形中某一点画一条水平线到图形外,若这条线与图形边线的交点为奇数则说明此点位于图形的内部;若交点为偶数,则此点在图形的外部。
Qt::WindingFill填充规则:从图形总某一点画一条水平线到图形外,每个交点外边线方向可能向上、向下、方向相反的相互抵消,若结果不为0表此点在图形内,若为0则在图形外。其中边线的方向是由QPainterPath创建时根据描述的顺序决定的,如果采用addRect(),或addPolygon()等函数加入的图形默认为顺时针方向。
#ifndef PAINTAREA_H
#define PAINTAREA_H#include
#includeclass 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
#include "paintarea.h"
#include
#includepaintArea::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;}
}
#ifndef TEXTPARINTER_H
#define TEXTPARINTER_H#include
#include"paintarea.h"
#include
#include
#include
#include
#include
#includeclass textParinter : public QDialog
{Q_OBJECTpublic: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
#include "textparinter.h"
#include"paintarea.h"
#includetextParinter::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(Qt::SolidLine));m_penStyleComboBox->addItem(tr("DashLine"),static_cast(Qt::DashLine));m_penStyleComboBox->addItem(tr("DotLine"),static_cast(Qt::DotLine));m_penStyleComboBox->addItem(tr("DashDotLine"),static_cast(Qt::DashDotLine));m_penStyleComboBox->addItem(tr("DashDotDotLine"),static_cast(Qt::DashDotDotLine));m_penStyleComboBox->addItem(tr("CustomDashLine"),static_cast(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(Qt::SolidPattern));m_brushStyleComboBox->addItem(tr("Dense1Pattern"),static_cast(Qt::Dense1Pattern));m_brushStyleComboBox->addItem(tr("Dense2Pattern"),static_cast(Qt::Dense2Pattern));m_brushStyleComboBox->addItem(tr("Dense3Pattern"),static_cast(Qt::Dense3Pattern));m_brushStyleComboBox->addItem(tr("Dense4Pattern"),static_cast(Qt::Dense4Pattern));m_brushStyleComboBox->addItem(tr("Dense5Pattern"),static_cast(Qt::Dense5Pattern));m_brushStyleComboBox->addItem(tr("Dense6Pattern"),static_cast(Qt::Dense6Pattern));m_brushStyleComboBox->addItem(tr("Dense7Pattern"),static_cast(Qt::Dense7Pattern));m_brushStyleComboBox->addItem(tr("HorPattern"),static_cast(Qt::HorPattern));m_brushStyleComboBox->addItem(tr("VerPattern"),static_cast(Qt::VerPattern));m_brushStyleComboBox->addItem(tr("CrossPattern"),static_cast(Qt::CrossPattern));m_brushStyleComboBox->addItem(tr("BDiagPattern"),static_cast(Qt::BDiagPattern));m_brushStyleComboBox->addItem(tr("FDiagPattern"),static_cast(Qt::FDiagPattern));m_brushStyleComboBox->addItem(tr("DiagCrossPattern"),static_cast(Qt::DiagCrossPattern));m_brushStyleComboBox->addItem(tr("LinearGradientPattern"),static_cast(Qt::LinearGradientPattern));m_brushStyleComboBox->addItem(tr("ConicalGradientPattern"),static_cast(Qt::ConicalGradientPattern));m_brushStyleComboBox->addItem(tr("RadialGradientPattern"),static_cast(Qt::RadialGradientPattern));m_brushStyleComboBox->addItem(tr("TexturePattern"),static_cast(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 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~1lineGradient.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));}}