PDA

View Full Version : StyleSheet question



chandan
2nd May 2010, 16:44
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

Lykurg
2nd May 2010, 16:48
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:
main() {
QApplication a(argc, argv);
a.setStyleSheet("QPushButton {color: #ff0000;}");
//...
return a.exec();
}

MorrisLiang
3rd May 2010, 03:00
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:

QFile qss(path);
qss.open(QFile::ReadOnly);
qApp->setStyleSheet(qss.readAll());
qss.close();

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"

Lykurg
3rd May 2010, 07:46
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.:eek:

MorrisLiang
3rd May 2010, 11:04
The 5th row in the table under selector types::eek:

ha,I didn't notice that...

FoleyX90
3rd May 2010, 16:24
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.

FoleyX90
3rd May 2010, 16:25
for instance, MyNewWindow *newWindow = MyNewWindow(this);