QPainter fill rectangle with gradient
Solved
General and Desktop
-
Hi,
i want to draw a rectangle filled with a linear gradient. My code:void Legend::paintEvent(QPaintEvent *) { QPainter painter(this); m_gradient.setColorAt(0.0, Qt::blue); m_gradient.setColorAt(1.0, Qt::red); painter.fillRect(QRect(0, 0, 20, height()), m_gradient); }
When painting the widget, the rectangle is only filled with the last color (red).
Where is my mistake?
-
Hi
You need to define the interpolation area
like
QLinearGradient m_gradient(0,0,0,100);
m_gradient.setColorAt(0.0, Qt::blue);
m_gradient.setColorAt(1.0, Qt::red);