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. Equivalent of GDIPlus::PathGradientBrush
QtWS25 Last Chance

Equivalent of GDIPlus::PathGradientBrush

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 188 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.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by Perdrix
    #1

    In Microsoft GDIPlus I might have this code:

      Graphics graphics(hdc);// structure  Graphics  object 
      Pen m_pen(Color::Blue,2);
      SolidBrush m_Brush(Color::Green);
      
      Point points[] = {
         Point(30, 30), Point(180, 30), Point(180, 150), Point(30, 150) };
    
      PathGradientBrush pathBrush(points,4);// Use points to create brushes 
    
      pathBrush.SetCenterColor(Color::Red);// Set center color 
    
      Color colors[] = {
         Color::Green ,Color::Black,Color::Yellow,Color::Blue};
      int count = 4;
      pathBrush.SetSurroundColors(colors, &count);// Set the border color 
    
      graphics.FillRectangle(&pathBrush, 30, 30, 150, 120);
    

    which would result in this:

    ad832faf-42aa-43cf-abd0-0be4607ce826-image.png

    How do I do that using Qt? I looked at QConicalGradient, but I couldn't see how I might use it to get that result :(

    Note that in my case I'm dealing with triangles not squares.

    Thanks
    David

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      I don't think there's a built in gradient type in Qt that can do that in one pass. The best would be to create your own gradient type I think, but if you're ok with a multi-pass approach you could blend multiple linear gradients like this:

      QRect r(0,0,200,200);
      QPainter p(some_paint_device);
      
      p.fillRect(r, Qt::red);
      
      QLinearGradient g(r.topLeft(), r.center());
      g.setColorAt(0,Qt::green);
      g.setColorAt(1,Qt::transparent);
      p.fillRect(r, g);
      
      g.setStart(r.topRight());
      g.setColorAt(0,Qt::black);
      p.fillRect(r, g);
      
      g.setStart(r.bottomRight());
      g.setColorAt(0,Qt::yellow);
      p.fillRect(r, g);
      
      g.setStart(r.bottomLeft());
      g.setColorAt(0,Qt::blue);
      p.fillRect(r, g);
      

      Obviously not a great solution if you have a lot of triangles to fill like that.

      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