Andy Shaw

Qt Commercial Support Weekly #11 - Stylesheets and CentOS

1/23/2012 11:31 AM  | Posted by: Andy Shaw
 

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.

10 

Comments:

charley | 1/24/2012 7:16 PM
We're using a stylesheet to customize touch-screen and desktop apps in *very* different ways (from a single physical stylesheet file that defines "common" colors, images, etc.) This "styling" is critical for touch-screen (for big buttons for fat fingers) and to accommodate changes when you do-or-don't have a mouse or keyboard. Stylesheets work. Biggest reason for stylesheets: Mouse input is a "precision device" (so "smaller" stylings work), while touch-screen has *no* precision input device (so stylings must be "bigger"). Do you have any thoughts about what we should be doing with Qt4.8/Qt5 for the QML-equivalent of stylesheets? While we like stylesheets (they work), we aren't sure how to centralize this type of styling in QML (as we start to make migration plans to QML).

Andy Shaw | 1/26/2012 12:50 AM
@charley: It is a good question actually, currently there is no stylesheet approach to things when it comes to QML but that is because how you go about changing elements in QML can be done with a different approach anyway. With QML you can specify different elements of your own that you can create the basic implementation of an element that will be styled the way you want and then add your other stuff on top of that. It could be that in the future there ends up being a way to do it on a global level in some form, but creating your own elements and making the style changes that way makes it easier to change them without touching the rest of the QML code. I hope this helps!

Dayana | 2/19/2012 10:32 AM
Well, failnly someone who categorizes the same way I do: First font-styles, then margin/padding and failnly border/background. And everything exotic after that.

Vanny | 2/21/2012 6:10 PM
Thanks for the great airctle. If everybody would follow a process like this when working in teams, would make thing so much easier. I'm going to implement this naming convention into my next project.

Jose | 3/1/2012 7:50 PM
Like Stephen above I too have retcnely started to alphabetise and have found it a much quicker and effective way of structuring my css. If I were to categorise I'd have to think about it too much and inevitably I'd get the order of my categories mixed up; alphabetically order is a no brainer.I've been building sites using css for over 8 years and I still don't see the benefits of a css reset (and certainly not one written by someone else). What I would advocate though is the use of a templated stylesheet containing styles you commonly reuse, for example how you set your body font, set img borders to zero etc.Indentation in stylesheets is nasty and I'm sorry but this is only going to make troubleshooting harder as I'll be sidescrolling in my preferred web editing tool.Why prefix all of your stylesheets with style_ ? Surely that goes against the whole idea of using a semantic naming convention? They're obviously stylesheets so remove the prefix.

Social Aliens | 4/12/2012 3:14 PM
hanks for the great airctle. If everybody would follow a process like this when working in teams, would make thing so much easier.

Reebo | 4/14/2012 12:12 PM
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.格安 航空券 海外

Adam | 5/11/2012 4:02 PM
Having said that they do offer some easy way to customize widgets that would ordinarily not be possible without considerable coding. Write my research paper

buy cheap cialis | 5/16/2012 8:37 PM
detbmo buy cheap cialis =-] cheap viagra %-[[[ viagra 8904 buy viagra online 8]]] cialis buy online rHHae

Payday advance | 5/16/2012 9:09 PM
yvkmmek Payday advance 9321 payday loans 1941 payday loans :-O

Add new comment:

User verification Image for user verification  
     

Tags

Archive

Authors

Pasi Matilainen

Pasi is a Software Specialist working at Digia, Qt Commercial R&D and he concentrates on Mac OS X development. Pasi holds an M.Sc. degree in Information Technology from the Tampere University of Technology, Finland.

Tarja Sundqvist

Tarja is a Senior Software Engineer in the Digia, Qt Commercial Support team. She has been working in Digia for over 10 years in various positions: software development, testing, error management. Now, Tarja is focusing on helping Qt Commercial customers with their daily Qt problems on Windows and Linux platforms. Tarja holds an M.Sc. degree in Information Processing Science from the University of Oulu, Finland.

Akseli Salovaara

Akseli is a Software Specialist at Digia, Qt Commercial R&D and is responsible for the Qt Commercial releases and deliveries. Akseli holds an B.Sc. degree in Information Technology from the University of Applied Sciences in Jyväskylä, Finland.

Samuli Piippo

Samuli is a Software Specialist at Digia, Qt Commercial R&D with a concentration on  embedded Linux and RTOS development. Samuli holds an M.Sc. degree in Information Processing Science from the University of Oulu, Finland.

Katherine Barrios

Katherine is the Marketing Manager at Digia, Qt Commercial. She is responsible for getting the word out about Qt Commercial to the Qt ecosystem and working together with our customers and the Qt community to further extend the love for Qt on desktop and embedded. She was previously employed at Nokia, Qt Development Frameworks as Program Marketing Manager and is based in Oslo, Norway.

Sami Makkonen

Sami is a Senior Product Manager working at Digia, Qt Commercial R&D and he is responsible for the product planning including new feature development and enhancements to existing functionality. Sami holds an M.Sc.(Econ.) degree in Computer Science.

Andy Shaw

Andy is the Head of Support at Digia, Qt Commercial and has been working with Qt and supporting customers using Qt for 11 years.  He thrives on solving customer problems and getting feedback from them.

Tuukka Turunen

Tuukka is the Director of R&D at Digia, Qt Commercial and is responsible for the planning, creation, verification and delivery of the Qt Commercial product. Tuukka holds a M.Sc.(Eng) and Licentiate of Technology degrees in Computer Science.

Qt Commercial Team