Search code examples
c++qtgradientqpainter

How to paint a QLinearGradient?


In "C++ GUI Programming with Qt 4" gradients are briefly mentioned, and there is a sample code that creates a QLinearGradient object, but they don't say how to actually draw it on a QPainter.

QLinearGradient gradient(50, 100, 300, 350);
gradient.setColorAt(0.0, Qt::white);
gradient.setColorAt(0.2, Qt::green);
gradient.setColorAt(1.0, Qt::black);

So after I've created the QLinearGradient with this code, how do I actually draw it on the QPainter?


Solution

  • You use it to construct a QBrush to use in a QPainter in your widget's paint event.