pyQT -QBrush 和 QPainter

QBrush

QBrush定义了QPainter的填充模式,具有样式、颜色、渐变以及纹理等属性。
画刷的style()定义了填充的样式,使用Qt::BrushStyle枚举,默认值是Qt::NoBrush,也就是不进行任何填充。我们可以从下面的图示中看到各种填充样式的区别:
在这里插入图片描述例:绘制半透明区域

painter = QPainter(self)
painter.setPen(QPen(Qt.white,0.6))
painter.setBrush(QColor(135,206,250, 60)) # 最后一位是设置透明属性(在0 - 255取值)
painter.drawRoundedRect(777, 156, 514, 89, 2, 2)  # 绘制圆角矩形
painter.setPen(QtCore.Qt.NoPen)
painter.setBrush(QtCore.Qt.NoBrush)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QPainter is a class in PyQt5 that provides a set of painting methods that can be used to draw on widgets, such as windows, buttons, and labels. It allows you to create custom graphics and visuals for your PyQt5 applications. Here are some common methods used in QPainter: - begin(): This method starts the painting process and specifies the device to be painted on. - end(): This method ends the painting process and releases the device. - setPen(): This method sets the pen color, width, and style used for drawing lines and outlines. - setBrush(): This method sets the brush color and style used for filling shapes. - drawText(): This method draws text on the widget. - drawLine(): This method draws a line on the widget. - drawRect(): This method draws a rectangle on the widget. - drawEllipse(): This method draws an ellipse on the widget. - drawArc(): This method draws an arc on the widget. To use QPainter, you need to create a QPainter object and call its methods to draw on the widget. Here's an example: ```python import sys from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtGui import QPainter, QColor, QBrush class MyWidget(QWidget): def paintEvent(self, event): painter = QPainter(self) painter.setPen(QColor(255, 0, 0)) painter.setBrush(QBrush(QColor(255, 255, 0))) painter.drawRect(10, 10, 100, 100) painter.drawText(50, 50, "Hello PyQt5") if __name__ == '__main__': app = QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec_()) ``` This code creates a custom widget that draws a red rectangle with a yellow fill and some text. The paintEvent() method is called whenever the widget needs to be repainted, and it creates a QPainter object to draw on the widget. The setPen() and setBrush() methods are used to set the pen and brush colors, and the drawRect() and drawText() methods are used to draw the rectangle and text, respectively.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值