In this Python PySide6 lesson we want to learn about Python PySide6 QConicalGradient, PySide6 provides QConicalGradient class which allows us to create a conical gradient with starting angle and a span angle. the gradient is drawn in a clockwise direction starting from the starting angle and continuing for the span angle.
This is an example of how to use QConicalGradient in PySide6 with object oriented programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
from PySide6.QtWidgets import QApplication, QWidget from PySide6.QtGui import QPainter, QConicalGradient from PySide6.QtCore import Qt class MyWidget(QWidget): def __init__(self): super().__init__() # set the x, y, width and height of window self.setGeometry(100, 100, 400, 400) #set the window title self.setWindowTitle("QConicalGradient - GeeksCoders.com") def paintEvent(self, event): painter = QPainter(self) # Create a conical gradient with a start angle of 0 and a span angle of 360 degrees gradient = QConicalGradient(self.width() / 2, self.height() / 2, 0) gradient.setColorAt(0, Qt.red) gradient.setColorAt(0.5, Qt.yellow) gradient.setColorAt(1, Qt.green) # Set the brush of the painter to the conical gradient painter.setBrush(gradient) # Draw rectangle with the gradient as the fill color painter.drawRect(0, 0, self.width(), self.height()) if __name__ == '__main__': app = QApplication([]) widget = MyWidget() widget.show() app.exec() |
In this example we have created QConicalGradient object with starting angle of 0 and span angle of 360 degrees, which creates full circle gradient. we set the color stops using the setColorAt method. and finally we set the brush of the painter to the QConicalGradient object and draw a rectangle with the gradient as the fill color.
Note that QConicalGradient inherits from QGradient, which means that you can use any of the methods available in the QGradient class to manipulate the gradient. For example, you can set the gradient transform using the setCoordinateMode and setGradientStops methods.
Run the complete code and this will be the result.
In result we can say that PySide6 provides powerful set of classes for creating and manipulating gradients in your Qt applications. QConicalGradient class is particularly useful for creating conical gradients that add visual interest and depth to your UI elements.
When using QConicalGradient class, it’s important to keep in mind that the gradient is defined by an angle that ranges from 0 to 360 degrees. this angle determines the starting and ending points of the gradient, as well as the direction in which the colors will blend.
By setting the center point and angle of the gradient, you can create different effects from simple color transitions to more complex designs. also you can use multiple gradient stops to create a gradient that blends between multiple colors.
Overall, the QConicalGradient class is nice and powerful tool for adding beautiful gradients to your PySide6 applications.