In this Python PySide6 lesson we want to learn about Python PySide6 QRadialGradient, PySide6 QRadialGradient is class used in PySide6 for creating radial color gradients. It is used in conjunction with QPainter object to draw complex shapes and images with multiple colors.
What is PySide6 QRadialGradient Color
QRadialGradient object is created by specifying the center point and radius of the gradient, as well as the colors and stops along the gradient. the stops are used to define the position of each color in the gradient, allowing for smooth transition between the colors.
In PySide6 QRadialGradient class is part of the QtGui module and can be imported using the following statement:
from PySide6.QtGui import QRadialGradient
QRadialGradient class provides several methods for defining and manipulating the gradient, including setColorAt() for setting the color at specific stop, setCenter() and setFocalPoint() for setting the center and focal point of the gradient and setSpread() for defining how the gradient should be spread beyond its center and radius.
QRadialGradient objects can be used to draw different shapes and images, including rectangles, ellipses, and paths. They can also be used to fill a region with a gradient, using the QPainter method setBrush().
In result we can say that QRadialGradient is powerful tool for creating dynamic and visually appealing graphics in PySide6 applications. It provides flexibility in defining the center and radius of the gradient and can be used to achieve a variety of visual effects.
This is an example of how to use QRadialGradient class in PySide6.
from PySide6.QtGui import QPainter, QColor, QRadialGradient
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QWidget, QApplication
#set window title
self.setWindowTitle("QRadialGradient - GeeksCoders.com")
#set the x, y, width and height of window
self.setGeometry(100, 100, 400, 400)
def paintEvent(self, event):
# create QPainter object
painter = QPainter(self)
# create QRadialGradient object
gradient = QRadialGradient(self.width() // 2, self.height() // 2, 150)
# add color stops to the gradient
gradient.setColorAt(0.2, QColor(255, 255, 0))
gradient.setColorAt(0.6, QColor(255, 0, 0))
# set the gradient as the brush for the painter
# draw a rectangle with the gradient as the brush
if __name__ == '__main__':
# create the application
app = QApplication(sys.argv)
# create an instance of our widget
widget = MyWidget()
# show the widget
# run the event loop
In this example first of all we have imported the required modules and classes, including QPainter, QColor, QRadialGradient,Qt, QWidget, QApplication and sys.
Next we have defined custom widget class called MyWidget, which inherits from QWidget. In the constructor, we call the constructor of the parent class using super().
In the paintEvent() method we create QPainter object and set the render hint to QPainter.Antialiasing, which improves the quality of the graphics.
After that we create QRadialGradient object, specifying the center point and radius of the gradient using the width() and height() methods of the widget. We also define the colors and stops along the gradient using setColorAt() method.
We set the brush of the QPainter object to the gradient using setBrush() method, and draw a rectangle using drawRect() method.
Finally, in the main block, we create an instance of MyWidget, set its geometry and show it using the show() method. We also start the event loop using app.exec() method.
This example creates a radial gradient with four colors and fills the rectangle of the widget with this gradient. The gradient starts with white color at the center, transitions to yellow at 20% distance from the center, then to red at 60% distance, and finally to black at the outer edge. The output will be a rectangular widget filled with this gradient.
Run the complete code and this will be the result.