PDA

View Full Version : How do you set a style for a whole widget?



paie
12th August 2011, 15:00
I tried this:


void ShiftOpGroup::setThisStyle()
{
QString style =
"QPushButton { "
"color: white; "
"background-color: "
"qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
"stop: 0 #08080a, stop: 1 #66777a); "
"border: 6px solid white; "
"border-style: sunken; border-width: 2px; "
"border-radius 6px; border-color: white; }"
"QPushButton:pressed { "
"color: black; background-color: aqua;"
"background-color: "
"qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
"stop: 0 #eaebfe, stop: 1 #76878a); "
"border-style: raised; border-width: 3px;"
"border-radius 4px; border-color: black;}";

this->setStyleSheet(style);
}


... 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 ...



class ShiftOpGroup : public QWidget
{
Q_OBJECT
public:
explicit ShiftOpGroup(QPoint *start, QWidget *parent = 0);

:
QButtonGroup *buttonGroup;
:

public slots:
void sbPressed(int);
void sbReleased(int);
:
}


ShiftOpGroup::ShiftOpGroup(QPoint *start, QWidget *parent) :
QWidget(parent)
{
:
connect(buttonGroup, SIGNAL(buttonPressed(int)), this, SLOT(sbPressed(int)));
connect(buttonGroup, SIGNAL(buttonReleased(int)), this, SLOT(sbReleased(int)));
:


void ShiftOpGroup::sbPressed(int pbIndex)
{
QString style =
"color: white; "
"background-color: "
"qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
"stop: 0 #08080a, stop: 1 #66777a); "
"border: 6px solid white; "
"border-style: sunken; border-width: 2px; "
"border-radius 6px; border-color: white; ";

pShiftButtons->widgetList[pbIndex]->setStyleSheet(style);}

void ShiftOpGroup::sbReleased(int pbIndex)
{
QString style =
"color: black; background-color: aqua;"
"background-color: "
"qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
"stop: 0 #eaebfe, stop: 1 #76878a); "
"border-style: raised; border-width: 3px;"
"border-radius 4px; border-color: black; ";

pShiftButtons->widgetList[pbIndex]->setStyleSheet(style);
}


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.

meazza
12th August 2011, 15:04
Why dont you set styleSheet in constructor?

paie
12th August 2011, 15:40
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?

Everall
12th August 2011, 17:19
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.

paie
12th August 2011, 18:52
OK, but what I don't understand from the documentation is how to incorporate the style sheets.

Consider. I have these two QPushButton styles.



QPushButton{
color:black;background-color:aqua;
background-color:
qlineargradient(x1:0,y1:0,x2:0,y2:1,
stop:0#eaebfe,stop:1#76878a);
border-style:raised;border-width:3px;
border-radius4px;border-color:black;
}

QPushButton:pressed {
color:white;
background-color:
qlineargradient(x1:0,y1:0,x2:0,y2:1,
stop:0#08080a,stop:1#66777a);
border:6pxsolidwhite;
border-style:sunken;border-width:2px;
border-radius6px;border-color:white;
}


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.



MySubClass::setThisStyle()
{
QString style =
"QPushButton{ "
"color:black;background-color:aqua;"
"background-color:"
"qlineargradient(x1:0,y1:0,x2:0,y2:1,"
"stop:0#eaebfe,stop:1#76878a);"
"border-style:raised;border-width:3px;"
"border-radius4px;border-color:black; }"
"QPushButton:pressed {"
"color:white;"
"background-color:"
"qlineargradient(x1:0,y1:0,x2:0,y2:1,"
"stop:0#08080a,stop:1#66777a);"
"border:6pxsolidwhite;"
"border-style:sunken;border-width:2px;"
"border-radius6px;border-color:white; }";

this->setStyleSheet(style);
}


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.

norobro
12th August 2011, 22:58
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.


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" :)
#include <QtGui>
class Widget : public QWidget
{
public:
Widget(QWidget *parent=0): QWidget(parent)
{
setThisStyle();
}

void setThisStyle() {
QString style =
"QPushButton{ "
"color:black;background-color:aqua;"
"background-color:"
"qlineargradient(x1:0,y1:0,x2:0,y2:1,"
"stop:0#eaebfe,stop:1#76878a);"
"min-width : 75; min-height : 15;"
"border-style:outset;border-width:2px;"
"border-radius4px;border-color:black; }"
"QPushButton:pressed {"
"color:white;"
"background-color:"
"qlineargradient(x1:0,y1:0,x2:0,y2:1,"
"stop:0#08080a,stop:1#66777a);"
"border:6pxsolidwhite;"
"border-style:inset;border-width:2px;"
"border-radius6px;border-color:white; }";
setStyleSheet(style);
}
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
QVBoxLayout lo;
lo.addWidget(new QPushButton("Button 1", &w));
lo.addWidget(new QPushButton("Button 2", &w));
w.setLayout(&lo);
w.show();
return a.exec();
}

#include "main.moc"

paie
12th August 2011, 23:17
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.

norobro
12th August 2011, 23:26
As Everall stated it's all explained in the style sheet reference here (http://doc.qt.nokia.com/4.7/stylesheet-reference.html)

For border style see this: link (http://doc.qt.nokia.com/4.7/stylesheet-reference.html#border-style)

wysota
12th August 2011, 23:53
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.

paie
13th August 2011, 00:15
As Everall stated it's all explained in the style sheet reference here (http://doc.qt.nokia.com/4.7/stylesheet-reference.html)

For border style see this: link (http://doc.qt.nokia.com/4.7/stylesheet-reference.html#border-style)

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!

wysota
13th August 2011, 09:10
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.