Results 1 to 4 of 4

Thread: QLinearGradient question

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QLinearGradient question

    Hi,

    I want to paint a Widget with a QLinearGradient:

    Qt Code:
    1. QLinearGradient qGradient;
    2.  
    3. QGradientStops qStops;
    4. QGradientStop qStop1(0.0,QColor(255,0,0));
    5. QGradientStop qStop2(0.5,QColor(255,255,255));
    6. QGradientStop qStop3(1.0,QColor(0,255,0));
    7.  
    8. qStops.append(qStop1);
    9. qStops.append(qStop2);
    10. qStops.append(qStop3);
    11. qGradient.setStops(qStops);
    12.  
    13. QPainter painter(this);
    14. painter.fillRect(rect(),qGradient);
    To copy to clipboard, switch view to plain text mode 


    What I want is a gradient from Red to White to Green but I only get a Green Widget.

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLinearGradient question

    You have to define the size and direction of the gradient.

    Qt Code:
    1. QLinearGradient grad(0,0,width(), height());
    2. grad.setColorAt(0, Qt::red);
    3. grad.setColorAt(0.5, Qt::white);
    4. grad.setColorAt(1, Qt::green);
    5.  
    6. QPainter p(this);
    7. p.fillRect(rect(), grad);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Dec 2007
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLinearGradient question

    try with

    Qt Code:
    1. QLinearGradient linGrad(0, 0, 40, 40);
    2. linGrad.setColorAt(0, Qt::red);
    3. linGrad.setColorAt(0.5, Qt::white);
    4. linGrad.setColorAt(1, Qt::green);
    5. linGrad.setSpread(QGradient::PadSpread);
    6. painter.setBrush(linGrad);
    7. painter.drawRect(0, 0, 40, 40);
    To copy to clipboard, switch view to plain text mode 

    n.b: here the x/y coords of rect is casual ....

    cheers
    Last edited by jpn; 8th March 2008 at 10:22. Reason: missing [code] tags

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QLinearGradient question

    Hi,

    Thanks,
    Òscar Llarch i Galán

Similar Threads

  1. Access to QSqlTableModel::isDirty Question.
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2007, 17:49
  2. Replies: 1
    Last Post: 15th March 2007, 20:45
  3. Question regarding how to paint after zoom.
    By JonathanForQT4 in forum Qt Programming
    Replies: 2
    Last Post: 26th January 2007, 15:34
  4. QThread exit()/quit() question
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 28th August 2006, 14:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.