Results 1 to 5 of 5

Thread: gradient background

  1. #1
    Join Date
    Jan 2006
    Posts
    39
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default gradient background

    Hello,
    If someone could help me with getting a gradient in the background of a button, I'd appreciate it.

    I would expect the following code to make the button's background be a gradient from red to white vertically from the top of the button to the bottom.

    Qt Code:
    1. QPushButton *b = new QPushButton("Hi", this);
    2.  
    3. QLinearGradient linearGrad(0, 0, 0, b->height());
    4. linearGrad.setColorAt(0, Qt::red);
    5. linearGrad.setColorAt(1, Qt::white);
    6.  
    7. QPalette palette = b->palette();
    8. palette.setBrush(QPalette::Button, QBrush(linearGrad));
    9. b->setPalette(palette);
    To copy to clipboard, switch view to plain text mode 

    All it does, though, is make the button black. Since it makes the button black, I know that I am successfully changing the palette and reapplying it, but I'm just not changing it in the right way.

    Any help would be great.

    Thanks,
    Derek

  2. #2
    Join Date
    Jan 2006
    Posts
    39
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: gradient background

    Can I only get this effect on a button by using a QStyle?

  3. #3
    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: gradient background

    You can always subclass the button and reimplement its paint event. Painting a button with a gradient is very tricky, so it's possible that it can't be done without subclassing.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

  5. #5
    Join Date
    Jan 2006
    Posts
    39
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: gradient background

    Thank you. I set my QStyle to CDE and it worked fine.

    Derek

Similar Threads

  1. Replies: 2
    Last Post: 10th February 2009, 13:12
  2. QLCDNumber with transparent background?
    By PolyVox in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2008, 05:34
  3. Replies: 5
    Last Post: 30th March 2008, 16:53
  4. background colour
    By kw in forum Qt Programming
    Replies: 6
    Last Post: 11th April 2006, 00:44
  5. Replies: 1
    Last Post: 5th April 2006, 16:44

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.