【qt】填充

填充

基本原理

  1. QPainter中使用的函数
    1. 使用画刷填充路径,填充后没有轮廓线条
      1. fillPath(const QPainterPath &path, const QBrush &brush)
      2. 使用该函数可绘制由fillPath()填充后,路径的轮廓线路
        1. strokePath(const QPainterPath &path, const QPen &pen)
      3. 使用画刷brush填充矩形区域,填充后没有轮廓线条
        1. fillRect(const QRectF &rectangle, const QBrush &brush)
        2. fillRect(const QRect &rectangle, const QBrush &brush)
        3. fillRect(int x, int y, int width, int height, const QBrush &brush)
      4. 使用颜色color填充矩形区域,填充后没有轮廓线条
        1. fillRect(const QRectF &rectangle, const QColor &color)
        2. fillRect(const QRect &rectangle, const QColor &color)
        3. fillRect(int x, int y, int width, int height, const QColor &color)
      5. 使用预定义的全局颜色color填充矩形区域,填充后没有轮廓线条
        1. fillRect(int x, int y, int width, int height, Qt::GlobalColor color)
        2. fillRect(const QRect &rectangle, Qt::GlobalColor color)
        3. fillRect(const QRectF &rectangle, Qt::GlobalColor color)
      6. 使用画刷样式填充矩形区域,填充后没有轮廓线条
        1. fillRect(int x, int y, int width, int height, Qt::BrushStyle style)
        2. fillRect(const QRect &rectangle, Qt::BrushStyle style)
        3. fillRect(const QRectF &rectangle, Qt::BrushStyle style)
      7. 擦除矩形区域的填充
        1. eraseRect(const QRectF &rectangle)
        2. eraseRect(int x, int y, int width, int height)
        3. eraseRect(const QRect &rectangle)
      8. 画刷原点。影响画刷纹理图案和渐变
        1. void setBrushOrigin(const QPointF &position)
        2. void setBrushOrigin(const QPoint &position)
        3. void setBrushOrigin(int x, int y)
        4. QPoint brushOrigin() const
      9. 背景画刷(透明模式(默认)下不起作用)
        1. void setBackground(const QBrush &brush)
        2. const QBrush &background() const
    2. 背景模式
      1. void setBackgroundMode(Qt::BGMode mode)
      2. Qt::BGMode backgroundMode() const
  2. 填充区域
    1. 当绘制封闭的形状时,在其内部可以填充各种颜色、图案等属性。但并不是所有封闭形状都可以被填充。
    2. 使用QPainter::drawLine()函数绘制的封闭形状就不能被填充
    3. 使用QPainter对象绘制的直线、弧不是填充区域
    4. 使用QPainter对象绘制的矩形、椭圆、弦、扇形是填充区域
    5. 使用路径QPainterPath与使用QPen绘制的图形和轮廓也是填充区域
    6. 填充区域不一定有轮廓线,比如使用Qt::NoPen画刷样式绘制的填充区域就没有轮廓线
    7. 使用画刷可向填充区域中填充颜色、图案、渐变等属性
    8. 要填充虚线的空白和字体背景,需要使用setBackground()函数设置背景画刷,且还需使用setBackgroundMode()函数把背景模式设置为Qt::OpaqueMode(不透明)
    9. 画刷原点对于使用渐变填充的区域和使用画刷填充纹理图案时会有一定影响

示例

#ifndef WIDGET_H
#define WIDGET_H

#include <QtWidgets>
#include <QLinearGradient>
class DrawGradient :public QWidget
{
    Q_OBJECT
private:
    void init(){

    }
protected:
    void paintEvent(QPaintEvent *event) override{
        Q_UNUSED(event)

        QPainter painter;
        painter.begin(this);

        QLinearGradient gradient(QPointF(11,11),QPointF(155,11));
        gradient.setColorAt(0,QColor(111,255,255)); //青色
        gradient.setColorAt(0.5,QColor(111,1,1)); //红色
        gradient.setColorAt(1,QColor(255,255,1)); //黄色
        gradient.setSpread(QGradient::RepeatSpread);//设置传播方式

        //创建两个画刷

        QBrush gradientBrush(gradient) , backgroundBrush(Qt::green);

        //设置画笔
        QPen pen;
        pen.setCapStyle(Qt::FlatCap);//端点模式
        pen.setWidth(10);
        pen.setStyle(Qt::DashLine);
        pen.setBrush(gradientBrush);
        painter.setPen(pen);

        //设置字体
        QFont font;
        font.setPointSize(33);
        painter.setFont(font);
        painter.setBackground(backgroundBrush); // 填充处空白处会变成绿色
        painter.setBackgroundMode(Qt::OpaqueMode);

        painter.drawText(11,111,"渐变字体");
        painter.setBrushOrigin(50,111);
        painter.drawRect(11,11,333,222);


        painter.end();
    }

public:
    DrawGradient(QWidget *p =nullptr) :QWidget(p){ init(); }
};

#endif // WIDGET_H
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值