Qt Commercial Support Weekly #11 - Stylesheets and CentOS


Quite a number of you out there are already using stylesheets and they are handy to use if you want to change the look of a widget in Qt without having to code your own style.  Although there are some limitations with using stylesheets as they cannot be mixed with custom styles and they will change the look of a widget,  even if you just change one part of it.  Of course, if you want to make it look as close as possible to the native style but with your changes, then this is possible but it depends on the original style as you may not have access to the theme engine which is used on some platforms to do the native styling.  If you want to provide your own look and feel for the application, then stylesheets are certainly a good way to go about that since the stylesheet itself will ensure that the styled widget looks the same across the platforms.  Having said that they do offer some easy way to customize widgets that would ordinarily not be possible without considerable coding.


For example, if you want to change how a spinbox appears in such a way that the arrow buttons appear underneath the spinbox rather than to the side of it, you can achieve this in two lines of stylesheet code:


QSpinBox::up-button { subcontrol-position: top left }
QSpinBox::down-button { subcontrol-position: top right }


Now that has positioned the buttons on the other side of the spinbox, however, they appear a bit smaller and there is a small gap.  So, what we can do here is, since the spinbox is using the box model inside for the stylesheet, we can move the buttons to have a different origin.  So if you add subcontrol-origin: margin to the two rules then it will adjust the spinbox buttons to go against the side and also take care of the gap between them too.  So now your stylesheet looks something like:


QSpinBox::up-button { subcontrol-origin: margin; subcontrol-position: top left }
QSpinBox::down-button { subcontrol-origin: margin; subcontrol-position: bottom left }


So, just with two lines of stylesheet code you can reposition parts of a widget, the same can be done for scrollbars and any widget that has subcontrols as a part of it.  So, this is a simple way to customize your widgets beyond just the usual font, color, margins and so on.  However, the general rule when it comes to stylesheets is always to remember that if you style part of a widget, then you usually have to style all of it, even if it is just positioning the subcontrols in their usual place.  For instance, in the example above with the moving of the QSpinBox buttons, the arrows themselves on the QSpinBox do not look as good as they could do, so we need to provide better pixmap representations of those arrows on the buttons so it looks better.  Of course we can provide whatever pixmap we want here as we could have a '+' and a '-' sign instead.  You are completely free to make the changes you see fit easily.


Just recently we have been noticing that there are some problems with CentOS (and potentially Red Hat too since they are similar) support out of the box for Qt 4.8 and for Qt Creator.  There is a problem with the QGtkStyle, which there is a patch for if you experience a crash at all relating to QScrollBar.  If you need that patch, then please contact us via the Qt Commercial suppport portal, it will be making it's way into Qt 4.8.1 in any case, so if you prefer to wait then it should be available then.  If you are using CentOS out of the box ,then you will need to upgrade your gcc version to 4.4 to be able to build Qt, but once you have done that then it will build just fine.


Additionally if you want to use Qt Creator then it will work however you will need to update a couple of packages in order to get the gdb debugger working in such a way so that you see the Qt objects inside Qt Creator nicely.  Otherwise you will be able to debug still but not see the extra information at a glance (such as the QString contents).  To get this working you need to update Python to 2.6.7 and then gdb to 4.3.1, once you have done this then Qt Creator should also work well out of the box too.


We do try to make the other platforms work out of the box as best as we can, but if you do experience any problems with them, then at the very least check if the compiler you are trying to use is supported.  If it is not, then a fair few of these problems can be solved by just upgrading the compiler to a supported one.