QPainter painter(this); // assume 'this' is a QWidget painter.fillRect(10, 10, 50, 50, Qt::red);
QPainter painter(this); QLinearGradient gradient(0, 0, 0, 100); gradient.setColorAt(0, Qt::white); gradient.setColorAt(1, Qt::blue); painter.fillRect(10, 10, 50, 50, gradient);In this example, we create a QPainter and a QLinearGradient object. The gradient starts at point (0,0) and ends at point (0,100), with a white-to-blue gradient. We then call fillRect with the same arguments as before, but instead of providing a solid color, we pass in the gradient object. This will fill the rectangle with the gradient we defined. Package library: The QPainter and fillRect functions are part of the Qt library.