void setBrush(const QBrush &brush);

在Qt中,void setBrush(const QBrush &brush); 是 QPainter 类的一个成员函数,用于设置画刷(QBrush)对象,该对象将用于接下来的绘制操作。QBrush 定义了如何填充形状的内部,包括颜色、样式(如实心、渐变或纹理)和其他属性。

当你在使用 QPainter 绘制形状(如矩形、椭圆等)时,你可以通过调用 setBrush 方法来指定形状的填充方式。例如,如果你想用红色实心填充一个矩形,你可以这样做:

QPainter painter(this); // 假设 'this' 是一个 QWidget 或者其他 QPaintDevice
QBrush redBrush(Qt::red); // 创建一个红色画刷
painter.setBrush(redBrush); // 设置画刷
painter.drawRect(10, 10, 100, 100); // 绘制一个红色填充的矩形

在这个例子中,setBrush 方法被用来指定一个红色的实心画刷。随后,当 drawRect 方法被调用时,它会使用这个红色画刷来填充矩形的内部。

QBrush 类非常灵活,支持多种填充样式,包括:

  • 实心颜色填充(Solid)
  • 渐变填充(LinearGradient, RadialGradient, ConicalGradient)
  • 纹理填充(Texture)

你可以根据需要创建不同类型的 QBrush 对象,并通过 setBrush 方法将其应用到 QPainter 上,以实现不同的视觉效果。

加上注释,不分隔/**************************************************************************** ** ** This file is part of the LibreCAD project, a 2D CAD program ** ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl) ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved. ** ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 as published by the Free Software ** Foundation and appearing in the file gpl-2.0.txt included in the ** packaging of this file. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this program; if not, write to the Free Software ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ** ** This copyright notice MUST APPEAR in all copies of the script! ** **********************************************************************/ #ifndef RS_PAINTERQT_H #define RS_PAINTERQT_H #include <QPainter> #include "rs_painter.h" /** * The Qt implementation of a painter. It can draw objects such as * lines or arcs in a widget. All coordinates are screen coordinates * and have nothing to do with the graphic view. */ class RS_PainterQt: public QPainter, public RS_Painter { public: RS_PainterQt( QPaintDevice* pd); virtual ~RS_PainterQt(); virtual void moveTo(int x, int y); virtual void lineTo(int x, int y); virtual void drawGridPoint(const RS_Vector& p); virtual void drawPoint(const RS_Vector& p); virtual void drawLine(const RS_Vector& p1, const RS_Vector& p2); //virtual void drawRect(const RS_Vector& p1, const RS_Vector& p2); virtual void fillRect ( const QRectF & rectangle, const RS_Color & color ); virtual void fillRect ( const QRectF & rectangle, const QBrush & brush ); virtual void drawArc(const RS_Vector& cp, double radius, double a1, double a2, const RS_Vector& p1, const RS_Vector& p2, bool reversed); virtual void drawArc(const RS_Vector& cp, double radius, double a1, double a2, bool reversed); virtual void drawArcMac(const RS_Vector& cp, double radius, double a1, double a2, bool reversed); virtual void drawCircle(const RS_Vector&, double radius); virtual void drawEllipse(const RS_Vector& cp, double radius1, double radius2, double angle, double a1, double a2, bool reversed); virtual void drawImg(QImage& img, const RS_Vector& pos, double angle, const RS_Vector& factor, int sx, int sy, int sw, int sh); virtual void drawTextH(int x1, int y1, int x2, int y2, const QString& text); virtual void drawTextV(int x1, int y1, int x2, int y2, const QString& text); virtual void fillRect(int x1, int y1, int w, int h, const RS_Color& col); virtual void fillTriangle(const RS_Vector& p1, const RS_Vector& p2, const RS_Vector& p3); virtual void drawPolygon(const QPolygon& a,Qt::FillRule rule=Qt::WindingFill); virtual void drawPath ( const QPainterPath & path ); virtual void erase(); virtual int getWidth(); virtual int getHeight(); virtual RS_Pen getPen(); virtual void setPen(const RS_Pen& pen); virtual void setPen(const RS_Color& color); virtual void setPen(int r, int g, int b); virtual void disablePen(); //virtual void setColor(const QColor& color); virtual void setBrush(const RS_Color& color); virtual void setClipRect(int x, int y, int w, int h); virtual void resetClipping(); protected: RS_Pen lpen; long rememberX; // Used for the moment because QPainter doesn't support moveTo anymore, thus we need to remember ourselve the moveTo positions long rememberY; }; #endif
最新发布
07-05
加上注释,不分隔/**************************************************************************** ** ** This file is part of the LibreCAD project, a 2D CAD program ** ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl) ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved. ** ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 as published by the Free Software ** Foundation and appearing in the file gpl-2.0.txt included in the ** packaging of this file. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this program; if not, write to the Free Software ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ** ** This copyright notice MUST APPEAR in all copies of the script! ** **********************************************************************/ #ifndef RS_PAINTER_H #define RS_PAINTER_H #include "rs_color.h" #include "rs_math.h" #include "rs_pen.h" #include "rs_vector.h" #include <QPainterPath> /** * This class is a common interface for a painter class. Such * a class will in it's implementation be responsible to paint * lines, arcs, ... in widgets. All angles in rad. * * Note that this is just an interface used as a slot to * communicate with the LibreCAD from a GUI level. This * does not contain any Qt or platform specific code. */ class RS_Painter { public: RS_Painter() { drawingMode = RS2::ModeFull; offset = RS_Vector(0.0,0.0); drawSelectedEntities=false; } virtual ~RS_Painter() {} /** * Sets the drawing mode. */ void setDrawingMode(RS2::DrawingMode m) { drawingMode = m; } // When set to true, only entities that are selected will be drawn void setDrawSelectedOnly(bool dso) { drawSelectedEntities=dso; } // When true, only selected items will be draw bool shouldDrawSelected() { return drawSelectedEntities; } /** * @return Current drawing mode. */ RS2::DrawingMode getDrawingMode() { return drawingMode; } virtual void moveTo(int x, int y) = 0; virtual void lineTo(int x, int y) = 0; virtual void drawGridPoint(const RS_Vector& p) = 0; virtual void drawPoint(const RS_Vector& p) = 0; virtual void drawLine(const RS_Vector& p1, const RS_Vector& p2) = 0; virtual void drawRect(const RS_Vector& p1, const RS_Vector& p2); virtual void drawArc(const RS_Vector& cp, double radius, double a1, double a2, const RS_Vector& p1, const RS_Vector& p2, bool reversed) = 0; virtual void drawArc(const RS_Vector& cp, double radius, double a1, double a2, bool reversed) = 0; void createArc(QPolygon& pa, const RS_Vector& cp, double radius, double a1, double a2, bool reversed); virtual void drawCircle(const RS_Vector& cp, double radius) = 0; virtual void drawEllipse(const RS_Vector& cp, double radius1, double radius2, double angle, double angle1, double angle2, bool reversed) = 0; virtual void drawImg(QImage& img, const RS_Vector& pos, double angle, const RS_Vector& factor, int sx, int sy, int sw, int sh) = 0; virtual void drawTextH(int x1, int y1, int x2, int y2, const QString& text) = 0; virtual void drawTextV(int x1, int y1, int x2, int y2, const QString& text) = 0; virtual void fillRect(int x1, int y1, int w, int h, const RS_Color& col) = 0; virtual void fillRect ( const QRectF & rectangle, const RS_Color & color ) = 0; virtual void fillRect ( const QRectF & rectangle, const QBrush & brush ) = 0; virtual void fillTriangle(const RS_Vector& p1, const RS_Vector& p2, const RS_Vector& p3) = 0; virtual void drawPath ( const QPainterPath & path ) = 0; virtual void drawHandle(const RS_Vector& p, const RS_Color& c, int size=-1); virtual RS_Pen getPen() = 0; virtual void setPen(const RS_Pen& pen) = 0; virtual void setPen(const RS_Color& color) = 0; virtual void setPen(int r, int g, int b) = 0; virtual void disablePen() = 0; virtual void setBrush(const RS_Color& color) = 0; virtual void drawPolygon(const QPolygon& a, Qt::FillRule rule=Qt::WindingFill) = 0; virtual void erase() = 0; virtual int getWidth() = 0; virtual int getHeight() = 0; virtual void setOffset(const RS_Vector& o) { offset = o; } virtual void setClipRect(int x, int y, int w, int h) = 0; virtual void resetClipping() = 0; int toScreenX(double x) { return RS_Math::round(offset.x + x); } int toScreenY(double y) { return RS_Math::round(offset.y + y); } protected: /** * Current drawing mode. */ RS2::DrawingMode drawingMode; /** * A fixed offset added to all entities drawn (useful for previews). */ RS_Vector offset; // When set to true, only selected entities should be drawn bool drawSelectedEntities; }; #endif
07-05
/**************************************************************************** ** ** This file is part of the LibreCAD project, a 2D CAD program ** ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl) ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved. ** ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 as published by the Free Software ** Foundation and appearing in the file gpl-2.0.txt included in the ** packaging of this file. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this program; if not, write to the Free Software ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ** ** This copyright notice MUST APPEAR in all copies of the script! ** **********************************************************************/ #ifndef RS_PAINTERQT_H #define RS_PAINTERQT_H #include <QPainter> #include "rs_painter.h" /** * RS_PainterQt 是基于 Qt 的绘图实现类。 * 它继承自 QPainter 和 RS_Painter,作为跨平台图形接口的具体实现。 * 所有方法将 RS_Painter 中的虚拟接口绑定到 Qt 绘图功能。 */ class RS_PainterQt: public QPainter,public RS_Painter { public: /** * 构造函数:绑定 QPainter 到指定的绘图设备(如 QWidget、QImage) * @param pd 绘图设备指针 */ RS_PainterQt(QPaintDevice* pd); /** * 析构函数 */ virtual ~RS_PainterQt(); // 实现基类 RS_Painter 的虚函数接口 /** * 移动画笔到指定坐标(用于绘制路径时记录起点) * @param x X 坐标 * @param y Y 坐标 */ virtual void moveTo(int x,int y); /** * 从当前点画线到目标坐标 * @param x X 坐标 * @param y Y 坐标 */ virtual void lineTo(int x,int y); /** * 绘制网格点(通常为小十字或圆点) * @param p 点位置向量 */ virtual void drawGridPoint(const RS_Vector& p); /** * 绘制普通点(根据当前画笔样式) * @param p 点位置向量 */ virtual void drawPoint(const RS_Vector& p); /** * 绘制线段 * @param p1 起点 * @param p2 终点 */ virtual void drawLine(const RS_Vector& p1,const RS_Vector& p2); /** * 填充矩形区域(使用颜色) * @param rectangle 矩形区域 * @param color 填充颜色 */ virtual void fillRect(const QRectF& rectangle,const RS_Color& color); /** * 填充矩形区域(使用画刷) * @param rectangle 矩形区域 * @param brush 填充画刷 */ virtual void fillRect(const QRectF& rectangle,const QBrush& brush); /** * 绘制弧线(带起终点) * @param cp 圆心 * @param radius 半径 * @param a1 起始角度 * @param a2 终止角度 * @param p1 起点坐标 * @param p2 终点坐标 * @param reversed 是否反向绘制 */ virtual void drawArc(const RS_Vector& cp,double radius, double a1,double a2, const RS_Vector& p1,const RS_Vector& p2, bool reversed); /** * 简化版绘制弧线 * @param cp 圆心 * @param radius 半径 * @param a1 起始角度 * @param a2 终止角度 * @param reversed 是否反向绘制 */ virtual void drawArc(const RS_Vector& cp,double radius, double a1,double a2, bool reversed); /** * Mac 平台专用弧线绘制方法 * @param cp 圆心 * @param radius 半径 * @param a1 起始角度 * @param a2 终止角度 * @param reversed 是否反向绘制 */ virtual void drawArcMac(const RS_Vector& cp,double radius, double a1,double a2, bool reversed); /** * 绘制圆形 * @param cp 圆心 * @param radius 半径 */ virtual void drawCircle(const RS_Vector&,double radius); /** * 绘制椭圆 * @param cp 椭圆中心 * @param radius1 长轴半径 * @param radius2 短轴半径 * @param angle 整体旋转角度 * @param a1 起始角度 * @param a2 终止角度 * @param reversed 是否反向绘制 */ virtual void drawEllipse(const RS_Vector& cp, double radius1,double radius2, double angle, double a1,double a2, bool reversed); /** * 绘制图像 * @param img 图像数据 * @param pos 位置 * @param angle 旋转角度 * @param factor 缩放因子 * @param sx 图像裁剪区X起点 * @param sy 图像裁剪区Y起点 * @param sw 图像裁剪宽度 * @param sh 图像裁剪高度 */ virtual void drawImg(QImage& img,const RS_Vector& pos, double angle,const RS_Vector& factor, int sx,int sy,int sw,int sh); /** * 水平方向绘制文本 * @param x1 起始X坐标 * @param y1 起始Y坐标 * @param x2 结束X坐标 * @param y2 结束Y坐标 * @param text 文本内容 */ virtual void drawTextH(int x1,int y1,int x2,int y2, const QString& text); /** * 垂直方向绘制文本 * @param x1 起始X坐标 * @param y1 起始Y坐标 * @param x2 结束X坐标 * @param y2 结束Y坐标 * @param text 文本内容 */ virtual void drawTextV(int x1,int y1,int x2,int y2, const QString& text); /** * 填充矩形(通过坐标和尺寸) * @param x1 起始X坐标 * @param y1 起始Y坐标 * @param w 宽度 * @param h 高度 * @param col 填充颜色 */ virtual void fillRect(int x1,int y1,int w,int h, const RS_Color& col); /** * 填充三角形 * @param p1 第一点 * @param p2 第二点 * @param p3 第三点 */ virtual void fillTriangle(const RS_Vector& p1, const RS_Vector& p2, const RS_Vector& p3); /** * 绘制多边形 * @param a 多边形顶点集合 * @param rule 填充规则(默认 Qt::WindingFill) */ virtual void drawPolygon(const QPolygon& a,Qt::FillRule rule = Qt::WindingFill); /** * 绘制 QPainterPath 类型的路径对象 * @param path 路径对象 */ virtual void drawPath(const QPainterPath& path); /** * 清除整个绘图区域(设置为背景色) */ virtual void erase(); /** * 获取当前绘图区域的宽度 * @return 宽度值 */ virtual int getWidth(); /** * 获取当前绘图区域的高度 * @return 高度值 */ virtual int getHeight(); /** * 获取当前画笔信息 * @return 当前 RS_Pen 对象 */ virtual RS_Pen getPen(); /** * 设置画笔样式 * @param pen 新的画笔样式 */ virtual void setPen(const RS_Pen& pen); /** * 设置画笔颜色 * @param color 颜色 */ virtual void setPen(const RS_Color& color); /** * 使用RGB分量设置画笔颜色 * @param r 红色 * @param g 绿色 * @param b 蓝色 */ virtual void setPen(int r,int g,int b); /** * 禁用画笔(不绘制轮廓线) */ virtual void disablePen(); /** * 设置填充颜色 * @param color 填充颜色 */ virtual void setBrush(const RS_Color& color); /** * 设置裁剪矩形区域 * @param x X坐标 * @param y Y坐标 * @param w 宽度 * @param h 高度 */ virtual void setClipRect(int x,int y,int w,int h); /** * 取消当前裁剪限制 */ virtual void resetClipping(); protected: RS_Pen lpen; // 当前使用的画笔缓存 long rememberX; // 记录上一个 moveTo 的X坐标(解决Qt某些版本问题) long rememberY; // 记录上一个 moveTo 的Y坐标 }; #endif把要实现的函数和需要的成员变量提取出来
07-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值