I shall start with a brief intro on Qt’s theming/styling and different ways they can be controlled at run-time and go through with some example and references:
Look and feel (theme) of Qt applications can be changed either by implementing custom style class inherited from QStyle or by using one of the predefined Style classes supplied with Qt. For example you can set your applications theme to Motif by calling
qApp->setStyle(new QMotifStyle);
Here QMotifStyle comes inbuilt with Qt.
You also can create your custom theme by deriving from one of the predefined style classes and overriding the virtual methods like drawPrimitive(..) accordingly. More detailed discussion on the same can be found here,
http://doc.trolltech.com/4.6/style-reference.html
A sample example is also supplied with Qt installation at \examples\widgets\styles (http://doc.trolltech.com/4.6/widgets-styles.html)
While the above method allows us to change theme at runtime and the custom style class to be statistically linked with the application during build. There is also a way we can implement the custom style as Qt plugin and place it under \plugins\styles folder so that the style can be loaded at runtime and is available to any arbitrary Qt based application.
We can set this theme to any qt application by passing a command line argument like this,
Myapplication.exe –style custompluginstyle
The application will use the look and feel from the custom style you implemented.
Along with what’s possible by subclassing QStyle, there is another robust way of controlling your applications s look and feel using stylesheets which are very similar to HTML CSS. For example we can change theme of all buttons by doing like this,
QPushButton {
border: 2px solid #8f8f91;
border-radius: 6px;
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #f6f7fa, stop: 1 #dadbde);
min-width: 80px;
}
QPushButton:pressed {
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #dadbde, stop: 1 #f6f7fa);
}
QPushButton:flat {
border: none; /* no border for a flat push button */
}
QPushButton:default {
border-color: navy; /* make the default button prominent */
}
Similarly we can change style of almost all the widgets and keep all of them in a qss file which can be loaded later at runtime like this,
QFile file("
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());
And do call QApplication::setStyleSheet(..) ,
qApp->setStyleSheet(styleSheet);
More detailed discussion on stylsheets can be found here, http://qt.nokia.com/doc/4.6/stylesheet.html
Another interesting discussion on this topic here,
http://doc.trolltech.com/qq/qq09-q-and-a.html
