QPainter fill rectangle with gradient



  • 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?


  • Qt Champions 2017

    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);



  • @mrjj
    Yeah make sense.

    Thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.