Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPainter fill rectangle with gradient
QtWS25 Last Chance

QPainter fill rectangle with gradient

Scheduled Pinned Locked Moved Solved General and Desktop
qpaintergradient
3 Posts 2 Posters 8.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • beeckscheB Offline
    beeckscheB Offline
    beecksche
    wrote on last edited by beecksche
    #1

    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      beeckscheB 1 Reply Last reply
      5
      • mrjjM mrjj

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

        beeckscheB Offline
        beeckscheB Offline
        beecksche
        wrote on last edited by
        #3

        @mrjj
        Yeah make sense.

        Thanks!

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved