Results 1 to 7 of 7

Thread: StyleSheet question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2010
    Posts
    69
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default StyleSheet question

    Hi Everyone,
    I am using Qt for a project. Now I need to do some styling to my UI. I think using Style Sheet could be useful for me. So if I want to use the same color and and background style then I have to use QPushButton{color: lightblue;} for all the buttons available in my UI. Can anyone tell me how to declare this stylesheet once and can be used for any buttons please? I know this is silly question but I am really stuck on it now. Another thing to ask I need to have some example to use gradient in the style sheet. Any help will be appreciated.
    Thanks in advance.
    Chandan

  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: StyleSheet question

    load a file or hardcode you css in a QString then fetch the main application via qApp() and use QApplication::setStyleSheet(). Place it at best in the constructor of you main window or in your main function.

    Example:
    Qt Code:
    1. main() {
    2. QApplication a(argc, argv);
    3. a.setStyleSheet("QPushButton {color: #ff0000;}");
    4. //...
    5. return a.exec();
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lykurg; 2nd May 2010 at 16:51. Reason: updated contents

  3. #3
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: StyleSheet question

    You should check out the chapter about StyleSheet in Qt Assistant.
    Just like Lykurg said,you can load a css/qss dynamically or hard code it.

    here is the dynamic method:
    Qt Code:
    1. QFile qss(path);
    2. qss.open(QFile::ReadOnly);
    3. qApp->setStyleSheet(qss.readAll());
    4. qss.close();
    To copy to clipboard, switch view to plain text mode 

    QPushButton{color: lightblue;} Means all QPushButtons' color is lightblue.
    QMainWindow > QPushButton{color: lightblue;} Means to style all QPushButtons which are direct children of QMainWindow.
    There're more seletor rules.
    And one isn't documented is
    #myBtn{background-color: blue} The # means to apply this style to the widget whose name is "myBtn"
    It's not the goodbye that hurts,but the flashback that follow.

  4. #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: StyleSheet question

    Quote Originally Posted by MorrisLiang View Post
    And one isn't documented is
    #myBtn{background-color: blue} The # means to apply this style to the widget whose name is "myBtn"
    The 5th row in the table under selector types:
    D Selector QPushButton#okButton Matches all QPushButton instances whose object name is okButton.

  5. #5
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: StyleSheet question

    Quote Originally Posted by Lykurg View Post
    The 5th row in the table under selector types:
    ha,I didn't notice that...
    It's not the goodbye that hurts,but the flashback that follow.

  6. #6
    Join Date
    Apr 2010
    Posts
    37
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: StyleSheet question

    If you do QPushButton{style goes here} in your QMainWindow stylesheet, all your QPushButtons will be the same style in that window and if you create new windows as a subclass of this window.

  7. #7
    Join Date
    Apr 2010
    Posts
    37
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: StyleSheet question


Similar Threads

  1. stylesheet
    By BalaQT in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2010, 07:45
  2. stylesheet question
    By gt.beta2 in forum Newbie
    Replies: 3
    Last Post: 30th January 2009, 16:27
  3. Qt Stylesheet
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 22nd January 2009, 14:48
  4. How to do this with StyleSheet
    By yxmaomao in forum Qt Programming
    Replies: 5
    Last Post: 30th May 2008, 08:54
  5. stylesheet
    By phillip_Qt in forum Qt Programming
    Replies: 11
    Last Post: 27th April 2008, 19:11

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
  •  
Qt is a trademark of The Qt Company.