Here are some predefined QbrushStyle for Qbrush, I am wondering is there any chance I can customize a style follow my own will. Thank you.
Asked
Active
Viewed 669 times
1 Answers
2
You have to create a QPixmap that represents the pattern and set it as a texture to the QBrush:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
def create_texture():
pixmap = QtGui.QPixmap(QtCore.QSize(8, 8))
pixmap.fill(QtGui.QColor("red"))
painter = QtGui.QPainter(pixmap)
painter.setBrush(QtGui.QBrush(QtGui.QColor("blue")))
painter.drawEllipse(pixmap.rect())
painter.end()
return pixmap
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
texture = create_texture()
brush = QtGui.QBrush()
brush.setTexture(texture)
scene = QtWidgets.QGraphicsScene()
view = QtWidgets.QGraphicsView(scene)
it = scene.addRect(QtCore.QRectF(0, 0, 400, 400))
it.setBrush(brush)
view.resize(640, 480)
view.show()
sys.exit(app.exec_())
Or QImage:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
def create_texture():
image = QtGui.QImage(QtCore.QSize(8, 8), QtGui.QImage.Format_ARGB32)
image.fill(QtGui.QColor("red"))
painter = QtGui.QPainter(image)
painter.setBrush(QtGui.QBrush(QtGui.QColor("blue")))
painter.drawEllipse(image.rect())
painter.end()
return image
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
texture = create_texture()
brush = QtGui.QBrush()
brush.setTextureImage(texture)
scene = QtWidgets.QGraphicsScene()
view = QtWidgets.QGraphicsView(scene)
it = scene.addRect(QtCore.QRectF(0, 0, 400, 400))
it.setBrush(brush)
view.resize(640, 480)
view.show()
sys.exit(app.exec_())

eyllanesc
- 235,170
- 19
- 170
- 241
-
Thank you. That's cool. I am involving in a gaze-contingent display project, I actually want to customize a blur style Qbrush. I have tried using QGraphicsBlurEffect to achieve my purpose, However, when using QGraphicsBlurEffect to a huge QGraphicsEllipseItem (circle, radius>2500px), the rendering delay becomes obvious, I have tested QGraphicsBlurEffect to relatively small QGraphicsItem, It's work well. so I want to define my own blur style Qbrush, any Idea? Thank you. – M conrey Jul 09 '20 at 05:39