Results 1 to 11 of 11

Thread: How do you set a style for a whole widget?

  1. #1
    Join Date
    Aug 2011
    Posts
    33
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How do you set a style for a whole widget?

    I tried this:
    Qt Code:
    1. void ShiftOpGroup::setThisStyle()
    2. {
    3. QString style =
    4. "QPushButton { "
    5. "color: white; "
    6. "background-color: "
    7. "qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
    8. "stop: 0 #08080a, stop: 1 #66777a); "
    9. "border: 6px solid white; "
    10. "border-style: sunken; border-width: 2px; "
    11. "border-radius 6px; border-color: white; }"
    12. "QPushButton:pressed { "
    13. "color: black; background-color: aqua;"
    14. "background-color: "
    15. "qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
    16. "stop: 0 #eaebfe, stop: 1 #76878a); "
    17. "border-style: raised; border-width: 3px;"
    18. "border-radius 4px; border-color: black;}";
    19.  
    20. this->setStyleSheet(style);
    21. }
    To copy to clipboard, switch view to plain text mode 

    ... but that does nothing.

    What I've had to do is to connect the "pressed" and "released" signals of each QPushButton to a slot that sets the style.

    Like this ...

    Qt Code:
    1. class ShiftOpGroup : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit ShiftOpGroup(QPoint *start, QWidget *parent = 0);
    6.  
    7. :
    8. QButtonGroup *buttonGroup;
    9. :
    10.  
    11. public slots:
    12. void sbPressed(int);
    13. void sbReleased(int);
    14. :
    15. }
    16.  
    17.  
    18. ShiftOpGroup::ShiftOpGroup(QPoint *start, QWidget *parent) :
    19. QWidget(parent)
    20. {
    21. :
    22. connect(buttonGroup, SIGNAL(buttonPressed(int)), this, SLOT(sbPressed(int)));
    23. connect(buttonGroup, SIGNAL(buttonReleased(int)), this, SLOT(sbReleased(int)));
    24. :
    25.  
    26.  
    27. void ShiftOpGroup::sbPressed(int pbIndex)
    28. {
    29. QString style =
    30. "color: white; "
    31. "background-color: "
    32. "qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
    33. "stop: 0 #08080a, stop: 1 #66777a); "
    34. "border: 6px solid white; "
    35. "border-style: sunken; border-width: 2px; "
    36. "border-radius 6px; border-color: white; ";
    37.  
    38. pShiftButtons->widgetList[pbIndex]->setStyleSheet(style);}
    39.  
    40. void ShiftOpGroup::sbReleased(int pbIndex)
    41. {
    42. QString style =
    43. "color: black; background-color: aqua;"
    44. "background-color: "
    45. "qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
    46. "stop: 0 #eaebfe, stop: 1 #76878a); "
    47. "border-style: raised; border-width: 3px;"
    48. "border-radius 4px; border-color: black; ";
    49.  
    50. pShiftButtons->widgetList[pbIndex]->setStyleSheet(style);
    51. }
    To copy to clipboard, switch view to plain text mode 

    I have to do that for every QPushButton in every subclass.

    There's got to be a better way.

    I tried doing it application-wide, and subclass-wide, but it does not seem to work unless I capture the "pressed" and "released" signals for each QPushButton.

  2. #2
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do you set a style for a whole widget?

    Why dont you set styleSheet in constructor?

  3. #3
    Join Date
    Aug 2011
    Posts
    33
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do you set a style for a whole widget?

    Thank you for the reply, really. This would probably be impossible without the help.

    Well, it was in the constructor of the subclass where I made the call to "setThisStyle();

    Can I not make a procedure call to set a style from the constructor?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    99
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How do you set a style for a whole widget?

    Normally you use your qapplication in your main and use setstylesheet there. Then it wil be used throughout your application. I think that is what you wat isn't it?

    I suggest you go to the docs, type in style sheet in the search tool and read the stylesheet reference. It's all explained there. Also the style sheet examples are a good help.

  5. #5
    Join Date
    Aug 2011
    Posts
    33
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do you set a style for a whole widget?

    OK, but what I don't understand from the documentation is how to incorporate the style sheets.

    Consider. I have these two QPushButton styles.

    Qt Code:
    1. color:black;background-color:aqua;
    2. background-color:
    3. qlineargradient(x1:0,y1:0,x2:0,y2:1,
    4. stop:0#eaebfe,stop:1#76878a);
    5. border-style:raised;border-width:3px;
    6. border-radius4px;border-color:black;
    7. }
    8.  
    9. QPushButton:pressed {
    10. color:white;
    11. background-color:
    12. qlineargradient(x1:0,y1:0,x2:0,y2:1,
    13. stop:0#08080a,stop:1#66777a);
    14. border:6pxsolidwhite;
    15. border-style:sunken;border-width:2px;
    16. border-radius6px;border-color:white;
    17. }
    To copy to clipboard, switch view to plain text mode 

    I can't find any way to incorporate these stylesheets outside of making them into QStrings and using this->setStyleSheet(). Now, I have no problem with that, except that all I get is the default pushbutton state styles if I try somthing like this.

    Qt Code:
    1. MySubClass::setThisStyle()
    2. {
    3. QString style =
    4. "QPushButton{ "
    5. "color:black;background-color:aqua;"
    6. "background-color:"
    7. "qlineargradient(x1:0,y1:0,x2:0,y2:1,"
    8. "stop:0#eaebfe,stop:1#76878a);"
    9. "border-style:raised;border-width:3px;"
    10. "border-radius4px;border-color:black; }"
    11. "QPushButton:pressed {"
    12. "color:white;"
    13. "background-color:"
    14. "qlineargradient(x1:0,y1:0,x2:0,y2:1,"
    15. "stop:0#08080a,stop:1#66777a);"
    16. "border:6pxsolidwhite;"
    17. "border-style:sunken;border-width:2px;"
    18. "border-radius6px;border-color:white; }";
    19.  
    20. this->setStyleSheet(style);
    21. }
    To copy to clipboard, switch view to plain text mode 

    The only way I've been able to express the two different styles for the "pressed" and "released" states is to map those signals to slots to set the styles accordingly.

    There must be a better way.

  6. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How do you set a style for a whole widget?

    I'd like to help but I'm pretty confused about what you are trying to achieve. Maybe instead of snippets of code you could provide a minimal compilable example.

    Quote Originally Posted by paie View Post
    Can I not make a procedure call to set a style from the constructor?
    Sure you can. The following works with your style sheet string. I changed the invalid "border-styles"
    Qt Code:
    1. #include <QtGui>
    2. class Widget : public QWidget
    3. {
    4. public:
    5. Widget(QWidget *parent=0): QWidget(parent)
    6. {
    7. setThisStyle();
    8. }
    9.  
    10. void setThisStyle() {
    11. QString style =
    12. "QPushButton{ "
    13. "color:black;background-color:aqua;"
    14. "background-color:"
    15. "qlineargradient(x1:0,y1:0,x2:0,y2:1,"
    16. "stop:0#eaebfe,stop:1#76878a);"
    17. "min-width : 75; min-height : 15;"
    18. "border-style:outset;border-width:2px;"
    19. "border-radius4px;border-color:black; }"
    20. "QPushButton:pressed {"
    21. "color:white;"
    22. "background-color:"
    23. "qlineargradient(x1:0,y1:0,x2:0,y2:1,"
    24. "stop:0#08080a,stop:1#66777a);"
    25. "border:6pxsolidwhite;"
    26. "border-style:inset;border-width:2px;"
    27. "border-radius6px;border-color:white; }";
    28. setStyleSheet(style);
    29. }
    30. };
    31.  
    32. int main(int argc, char *argv[])
    33. {
    34. QApplication a(argc, argv);
    35. Widget w;
    36. lo.addWidget(new QPushButton("Button 1", &w));
    37. lo.addWidget(new QPushButton("Button 2", &w));
    38. w.setLayout(&lo);
    39. w.show();
    40. return a.exec();
    41. }
    42.  
    43. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to norobro for this useful post:

    paie (12th August 2011)

  8. #7
    Join Date
    Aug 2011
    Posts
    33
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do you set a style for a whole widget?

    Wow! Yes! THAT works!!

    Thank you very Much!

    But, how do you know what is a valid value for the borders and what is not? I cannot seem to grep that from the documentation.

  9. #8
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How do you set a style for a whole widget?

    As Everall stated it's all explained in the style sheet reference here

    For border style see this: link

  10. The following user says thank you to norobro for this useful post:

    paie (12th August 2011)

  11. #9
    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: How do you set a style for a whole widget?

    There is also an option to pass an external stylesheet as a command line switch to the executable. This enables one to play with stylesheets without having to recompile anything. How to do that is left to the curiosity of the reader.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following user says thank you to wysota for this useful post:

    paie (12th August 2011)

  13. #10
    Join Date
    Aug 2011
    Posts
    33
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do you set a style for a whole widget?

    Quote Originally Posted by norobro View Post
    As Everall stated it's all explained in the style sheet reference here

    For border style see this: link
    Thank you very much for the links. I was really unable to locate these references in the docs, but now that I have the correct key words, I see that they are there.

    Thanks again!

    How to do that is left to the curiosity of the reader.
    Hmmm ... didn't know you could invoke the app from the command line.

    I see (argv, argc) in the main.c, so that must be it.

    Many thanks yet again!

  14. #11
    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: How do you set a style for a whole widget?

    Quote Originally Posted by paie View Post
    Hmmm ... didn't know you could invoke the app from the command line.
    You can invoke any app from the command line, that was not the point of what I said. The point is you can pass an external stylesheet to the app by means already provided by Qt.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. custom widget with the windows7 style titlebar??
    By pirlogy in forum Qt Programming
    Replies: 1
    Last Post: 21st May 2011, 00:30
  2. Style Sheet for custom widget
    By 1111 in forum Qt Programming
    Replies: 3
    Last Post: 31st August 2010, 12:52
  3. table widget style problem
    By zgulser in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2010, 12:23
  4. Get style sheet properties in own widget
    By Lykurg in forum Qt Programming
    Replies: 0
    Last Post: 24th June 2009, 14:18
  5. style widget on canvas (qgv)
    By valtovar in forum Qt Programming
    Replies: 1
    Last Post: 30th October 2008, 00:31

Tags for this Thread

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.