Python PySide6
About Lesson

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:

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.

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.

Python PySide6 QRadialGradient
Python PySide6 QRadialGradient