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
Forum Updated to NodeBB v4.3 + New Features

QPainter fill rectangle with gradient

Scheduled Pinned Locked Moved Solved General and Desktop
qpaintergradient
3 Posts 2 Posters 8.1k Views 2 Watching
  • 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.
  • B Offline
    B Offline
    beecksche
    wrote on 21 Jul 2016, 12:39 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 21 Jul 2016, 12:48 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);

      B 1 Reply Last reply 21 Jul 2016, 12:49
      5
      • M mrjj
        21 Jul 2016, 12:48

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

        B Offline
        B Offline
        beecksche
        wrote on 21 Jul 2016, 12:49 last edited by
        #3

        @mrjj
        Yeah make sense.

        Thanks!

        1 Reply Last reply
        1

        3/3

        21 Jul 2016, 12:49

        • Login

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