Results 1 to 7 of 7

Thread: background gradient with stylesheets

  1. #1
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default background gradient with stylesheets

    Hi,

    Im trying to accomplish something that I think is quite easy but I can't figure out how to use the gradients in the right way. What I'm trying to do is style a QPushButton background with stylesheets so it looks like the image below.

    This is what I want:



    This is the code I'm using but I never get the sharp line in the middle, it just fade over..
    Qt Code:
    1. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 0.5, x3: 0 y3: 1,
    2. stop: 0 #ABCD44, stop: 0.5 #ABCD44,
    3. stop: 0.5 #A1C72E, stop: 1.0 #9CC322);
    To copy to clipboard, switch view to plain text mode 

    I know I'm just doing something wrong when building the style but I'm not totally sure how to use the gradients.. Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: background gradient with stylesheets

    you have to use something like that:
    css Code:
    1. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
    2. stop: 0 #ABCD44,
    3. stop: 0.5 #ABCD44,
    4. stop: 0.51 #A1C72E,
    5. stop: 0.54 #A1C72E,
    6. stop: 1.0 #9CC322);
    To copy to clipboard, switch view to plain text mode 

    If you want a clear line you have to reimp the paint event and do the drawing yourself,

  3. The following user says thank you to Lykurg for this useful post:

    martinn (14th February 2010)

  4. #3
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default Re: background gradient with stylesheets

    Thanks very much now I got the background working! One thing though, when I select the button when running the app in the Symbian Emulator I get this white inner border like in this picture:

    Why is that and can I get rid of it?

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: background gradient with stylesheets

    I don't see a image, but it is because the button gets the focus and you see the focus rectangle. For that you have to alter the paint event and remove the flag has_focus from the option and then let Qt draw the button. Search the forum on that topic. It has been handled several times.

    EDIT: the correct flag is QStyle::State_HasFocus.
    Last edited by Lykurg; 15th February 2010 at 15:25.

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: background gradient with stylesheets

    A possible paintEvent would look like:
    Qt Code:
    1. void MyPushButton::paintEvent(QPaintEvent* event)
    2. {
    3. Q_UNUSED(event);
    4. QStylePainter painter(this);
    5. initStyleOption(&option);
    6. option.state &= ~QStyle::State_HasFocus;
    7. painter.drawControl(QStyle::CE_PushButton, option);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Or try to use style sheet with ":focus":
    css Code:
    1. QPushButton:focus{
    2. border: 0;
    3. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default Re: background gradient with stylesheets

    Hi again,

    aha, thats why! The rectangle shows up for many of the widgets when they have focus and I'm using stylesheets to style my app and it seems like a mass of work if I need to alter the paint event for each QPushbutton, QTabbar, QListWidgetItem and so on to get rid of this rectangle? Can I use stylesheets instead in some way? Setting the border of QPushButton:focus, didn't seem to solve it..

  8. #7
    Join Date
    Feb 2010
    Posts
    51
    Thanks
    9
    Thanked 14 Times in 4 Posts

    Default Re: background gradient with stylesheets

    Seems like setFocusPolicy(Qt::NoFocus) got rid of the rectangle but now the button wouldn't accept my stylesheet for:
    Code:

    QPushButton:selected {
    background: red;
    }

    So I can choose between having the ugly rectangle and be able to style the button when selected OR no rectangle but also no style when selected at all? Or am I doing something wrong here?

Similar Threads

  1. Replies: 5
    Last Post: 21st July 2010, 22:51
  2. getting gradient colors
    By venkat.godavarthi in forum Qt Programming
    Replies: 1
    Last Post: 30th October 2009, 13:36
  3. Replies: 1
    Last Post: 18th September 2009, 10:32
  4. Gradient in histogram
    By giusepped in forum Qwt
    Replies: 1
    Last Post: 1st August 2009, 11:48
  5. gradient background
    By drkbkr in forum Qt Programming
    Replies: 4
    Last Post: 30th March 2006, 16:10

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.