目录
一、简介
本实例介绍了各种形状的区分,以及画笔颜色、画笔线宽、画笔风格、画笔顶帽、画笔连接点、填充模式、铺展效果、画刷颜色和画刷风格设置等。
二、示例代码
mainwidget.h:
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QWidget>
#include "paintarea.h"
#include <QLabel>
#include <QComboBox>
#include <QSpinBox>
#include <QPushButton>
#include <QGridLayout>
#include <QGradient>
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
mainwidget.cpp:
#include "mainwidget.h"
#include <QColorDialog>
MainWidget::MainWidget(QWidget *parent)
: QWidget(parent)
{
paintArea =new PaintArea;
shapeLabel =new QLabel(tr("形状:")); //形状选择下拉列表框
shapeComboBox =new QComboBox;
shapeComboBox->addItem(tr("Line"),PaintArea::Line);
shapeComboBox->addItem(tr("Rectangle"),PaintArea::Rectangle);
shapeComboBox->addItem(tr("RoundedRect"),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("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(tr("画笔颜色:")); //画笔颜色选择控件
penColorFrame =new QFrame;
penColorFrame->setFrameStyle(QFrame::Panel|QFrame::Sunken);
penColorFrame->setAutoFillBackground(true);
penColorFrame->setPalette(QPalette(Qt::blue));
penColorBtn =new QPushButton(tr("更改"));
connect(penColorBtn,SIGNAL(clicked()),this,SLOT(ShowPenColor()));
penWidthLabel =new QLabel(tr("画笔线宽:")); //画笔线宽选择控件
penWidthSpinBox =new QSpinBox;
penWidthSpinBox->setRange(0,20);
connect(penWidthSpinBox,SIGNAL(valueChanged(int)),this,SLOT (ShowPenWidth(int)));
penStyleLabel =new QLabel(tr("画笔风格:")); //画笔风格选择下拉列表框
penStyleComboBox =new QComboBox;
penStyleComboBox->addItem(tr("SolidLine"),static_cast<int>(Qt::SolidLine));
penStyleComboBox->addItem(tr("DashLine"),static_cast<int>(Qt::DashLine));
penStyleComboBox->addItem(tr("DotLine"),static_cast<int>(Qt::DotLine));
penStyleComboBox->addItem(tr("DashDotLine"),static_cast<int>(Qt::DashDotLine));
penStyleComboBox->addItem(tr("DashDotDotLine"),static_cast<int>(Qt::DashDotDotLine));
penStyleComboBox->addItem(tr("CustomDashLine"),static_cast<int>(Qt::CustomDashLine));
connect(penStyleComboBox,SIGNAL(activated(int)),this,SLOT (ShowPenStyle(int)));
penCapLabel =new QLabel(tr("画笔顶帽:")); //画笔顶端风格选择下拉列表框
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(tr("画笔连接点:")); //画笔连接点风格选择下拉列表框
penJoinComboBox =new QComboBox;
penJoinComboBox->addItem(tr("BevelJoin"),Qt::BevelJoin);
penJoinComboBox->addItem(tr("MiterJoin"),Qt::MiterJoin);
penJoinComboBox->addItem(tr("RoundJoin"),Qt::RoundJoin);
connect(penJoinComboBox,SIGNAL(activated(int)),this,SLOT(ShowPenJoin (int)));
fillRuleLabel =new QLabel(tr("填充模式:")); //填充模式选择下拉列表框
fillRuleComboBox =new QComboBox;
fillRuleComboBox->addItem(tr("Odd Even"),Qt::OddEvenFill);
fillRuleComboBox->addItem(tr("Winding"),Qt::WindingFill);
connect(fillRuleComboBox,SIGNAL(activated(int)),this,SLOT (ShowFillRule()));
spreadLabel =new QLabel(tr("铺展效果:")); //铺展效果选择下拉列表框
spreadComboBox =new QComboBox;
spreadComboBox->addItem(tr("PadSpread"),QGradient::PadSpread);
spreadComboBox->addItem(tr("RepeatSpread"),QGradient::RepeatSpread);
spreadComboBox->addItem(tr("ReflectSpread"),QGradient:: ReflectSpread);
connect(spreadComboBox,SIGNAL(activated(int)),this,SLOT (ShowSpreadStyle()));
brushColorLabel =new QLabel(tr("画刷颜色:")); //画刷颜色选择控件
brushColorFrame =new QFrame;
brushColorFrame->setFrameStyle(QFrame::Panel|QFrame::Sunken);
brushColorFrame->setAutoFillBackground(true);
brushColorFrame->setPalette(QPalette(Qt::green));
brushColorBtn =new QPushButton(tr("更改"));
connect(brushColorBtn,SIGNAL(clicked()),this,SLOT (ShowBrushColor()));
brushStyleLabel =new QLabel(tr("画刷风格:")); //画刷风格选择下拉列表框
brushStyleComboBox =new QComboBox;
brushStyleComboBox->addItem(tr("SolidPattern"),static_cast<int>(Qt::SolidPattern));
brushStyleComboBox->addItem(tr("Dense1Pattern"),static_cast<int>(Qt::Dense1Pattern));
brushStyleComboBox->addItem(tr("Dense2Pattern"),static_cast<int>(Qt::Dense2Pattern));
brushStyleComboBox->addItem(tr("Dense3Pattern"),static_cast<int>(Qt::Dense3Pattern));
brushStyleComboBox->addItem(tr("Dense4Pattern"),static_cast<int>(Qt::Dense4Pattern));
brushStyleComboBox->addItem(tr("Dense5Pattern"),static_cast<int>(Qt::Dense5Pattern));
brushStyleComboBox->addItem(tr("Dense6Pattern"),static_cast<int>(Qt::Dense6Pattern));
brushStyleComboBox->addItem(tr("Dense7Pattern"),static_cast<int>(Qt::Dense7Pattern));
brushStyleComboBox->addItem(tr("HorPattern"),static_cast<int>(Qt::HorPattern));
brushStyleComboBox->addItem(tr("VerPattern"),static_cast<int>(Qt::VerPattern));
brushStyleComboBox->addItem(tr("CrossPattern"),static_cast<int>(Qt::CrossPattern));
brushStyleComboBox->addItem(tr("BDiagPattern"),static_cast<int>(Qt::BDiagPattern));
brushStyleComboBox->addItem(tr("FDiagPattern"),static_cast<int>(Qt::FDiagPattern));
brushStyleComboBox->addItem(tr("DiagCrossPattern"),static_cast<int>(Qt:: DiagCrossPattern));
brushStyleComboBox->addItem(tr("LinearGradientPattern"),static_cast<int>(Qt:: LinearGradientPattern));
brushStyleComboBox->addItem(tr("ConicalGradientPattern"),static_cast<int>(Qt:: ConicalGradientPattern));
brushStyleComboBox->addItem(tr("RadialGradientPattern"),static_cast<int>(Qt:: RadialGradientPattern));
brushStyleComboBox->addItem(tr("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());
paintArea->setShape(shape);
}
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::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 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 join=Qt::PenJoinStyle(penJoinComboBox->itemData(penJoinComboBox->currentIndex(),Qt::UserRole).toInt());
paintArea->setPen(QPen(color,value,style,cap,join));
}
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 join=Qt::PenJoinStyle(penJoinComboBox->itemData(penJoinComboBox->currentIndex(),Qt::UserRole).toInt());
paintArea->setPen(QPen(color,value,style,cap,join));
}
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 join=Qt::PenJoinStyle(penJoinComboBox->itemData(joinValue,Qt::UserRole).toInt());
paintArea->setPen(QPen(color,value,style,cap,join));
}
void MainWidget::ShowFillRule()
{
Qt::FillRule rule = Qt::FillRule(fillRuleComboBox->itemData(fillRuleComboBox->currentIndex(),Qt::UserRole).toInt());
paintArea->setFillRule(rule);
}
void MainWidget::ShowSpreadStyle()
{
spread = QGradient::Spread(spreadComboBox->itemData(spreadComboBox->currentIndex(),Qt::UserRole).toInt());
}
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,100);
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,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("butterfly.png")));
}
else
{
paintArea->setBrush(QBrush(color,style));
}
}
paintarea.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 = 0);
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
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)
{
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)
};
int startAngle =30*16;
int spanAngle =120*16;
QPainterPath path; //创建路径对象
path.addRect(150,150,100,100); //添加矩形
path.moveTo(100,100); //路径的当前绘制位置移动到 (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.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("butterfly.png")); break;
default: break;
}
}
main.cpp:
#include "mainwidget.h"
#include <QApplication>
#include <QFont>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFont f("ZYSong18030",12);
a.setFont(f);
MainWidget w;
w.show();
return a.exec();
}
三、demo详解
1、画笔(QPen)和刷子(QBrush)分别有什么作用?
a、画笔(QPen):
- 作用:画笔主要用于绘制线条和轮廓。它控制线条的宽度、样式(实线、虚线等)以及颜色。
- 属性:
- 宽度:可以设置线条的宽度。
- 样式:可以设置线条的样式,如实线、虚线、点线等。
- 颜色:设置线条的颜色
- 端点样式:设置线条的端点样式,如圆头、方头等。
- 连接样式:设置线条连接的样式,如圆角、斜角等。
b、刷子(QBrush):
- 作用:刷子主要用于填充图形的内部区域。它控制填充的颜色、图案或渐变效果。
- 属性:
- 颜色:可以设置填充的颜色。
- 图案:可以使用不同的图案来填充区域,如斜线、点阵等。
- 渐变:支持线性渐变、径向渐变等复杂的填充效果。
2、利用 QPainter 绘制图形 (Shape )
Qt 为开发者提供了丰富的绘制基本图形的 draw 函数:
QPainter类还提供了一个 drawPixmap()函数,可以直接将图片画到刻画控件上。
3、利用 QPainterPath 绘制简单图形
利用 QPainterPath 绘制简单图形,QPainterPath 类为 QPainter 类提供了一个存储容器,里面包含了所要绘制的内容的集合及绘制的顺序,如长方形、多边形、曲线等各种任意图形。当需要绘制此预先存储在 QPainterPath 对象中的内容时,只需调用QPainter 类的 drawPath()函数即可。
QPainterPath 类提供了许多函数接口,可以很方便地加入一些规则图形。例如,addRect()加入一个方形,addEllipse()加入一个椭圆形,addText()加入一个字符串,addPolygon()加入一个多边形等。同时, QPainterPath 类还提供了 addPath()函数,用于加入另一个QPainterPath 对象中保存的内容。
QPainterPath 对象的当前点自动处在上一部分图形内容的结束点上,若下一部分图形的起点不在此结束点,则需调用 moveTo()函数将当前点移动到下一部分图形的起点。
4、QComboBox::addItem
void QComboBox::addItem(const QString &atext, const QVariant &auserData)
const QString &atext: 这是要添加的项的显示文本,即下拉框中显示的文字。QString 是 Qt 的字符串类,atext 参数是传入的文本内容。
const QVariant &auserData: 这是与项相关联的用户数据。QVariant 是 Qt 提供的一个通用数据类型,可以存储各种类型的数据,如整数、浮点数、字符串等。如果不需要额外的数据,可以传递一个默认构造的 QVariant 对象。
#include <QComboBox>
#include <QVariant>
#include <QString>
#include <QDebug>
// 假设我们已经有一个 QComboBox 对象 comboBox
QComboBox *comboBox = new QComboBox;
// 添加一些项,其中包括用户数据
comboBox->addItem("Item 1", QVariant(123));
comboBox->addItem("Item 2");
// 获取第一项的用户数据(索引从0开始)
QVariant userData = comboBox->itemData(0);
// 打印出用户数据
qDebug() << "Item 1's user data:" << userData.toInt(); // 输出: 123
// 如果你尝试获取第二项的用户数据,但没有设置用户数据
QVariant userData2 = comboBox->itemData(1);
if (userData2.isValid()) {
qDebug() << "Item 2's user data:" << userData2.toString(); // 输出为空
} else {
qDebug() << "Item 2 has no user data."; // 输出: Item 2 has no user data.
}
关键点:
itemData(int index)
:此方法返回指定索引项的QVariant
数据。index
是要访问的项的索引,从 0 开始。QVariant
对象可以包含各种类型的数据,可以通过QVariant
提供的方法来获取具体的数据类型(如toInt()
、toString()
等)。
四、总结
本文简述了Qt绘画的一些相关知识,Qt绘画的相关知识还有很多。本文后续还会持续的完善。