Linux - Support system's theme change
I have just noticed that though my application works as I would expect on Linux (Ubuntu 11.04), if I change my system's theme, then my application doesn't fully handle the change. The window border decoration will respect the theme, but the menus, dialog buttons, etc. won't.
I have just checked with the Qt (4.7.3) demos (which I manually built on my Ubuntu 11.04 box) and it's the same. They, in fact, use the same (default?) theme as my application. However, everything works as I would expect using Qt Creator 2.2.1.
So, what is the difference between my application / the Qt demos and Qt Creator? How do I go about ensuring that my application respects the system's theme?
P.S.: I have just checked on Windows and Mac OS X, and as far as I can tell everything works as expected, i.e. my application handles changes in the system's theme without any problem.
What happens if you set the style of your program in code?
What style are you trying to use?
If I programmatically set a style, e.g.
then the style is properly set, but it obviously doesn't respond to any change to the system's theme, and is therefore not what I want.
In fact, I don't use and don't want to use any particular style. I just want my application to use the 'default' system style and respond to any change to it. Nothing more nothing less.
The problem is, that your app runs on gnome and this is gtk and not Qt. If you use KDE, your app will use the system theme!
I've used kubuntu 10.10 in the past and there it's ok. But that's kde.
Are you sure about that Scylla?
It seems strange to me.
A workaround could be to get the system style in code and set it like you did.
Here it works as expected (Kubuntu 1010). If you don't use a hard coded style, it should work on KDE.
You need to have a GTK style engine in Qt, so that Qt will use GTK styling by default. http://code.google.com/p/qgtkstyle/
EDIT: Okay, that link is outdated. But anyway, that piece of code allows to use GTK styles in Qt, I don’t know where is instructions to use it.
For what it is worth, I quickly installed Kubuntu 11.04, and ran my application on it, and its look didn't match that of the KDE theme. Then again, I built my application on Ubuntu 11.04 and copied it over to my Kubuntu system, so it might explain...? (Note: I don't use any hard-coded style and, in fact, don't want to have to do that at all, since my application may end up being use on different flavours of Linux.)
Eddy: what do you mean by "get the system style in code"?
Smar: I have seen that Qt has QGtkStyle class, but on my Ubuntu box, QT_NO_STYLE_GTK is defined and therefore QGtkStyle is not available. Then again, as I mentioned before, I don't want to have to use styles. I would just hope that things would somehow work... :)
There is an example called stylesheet that uses the solution I talked about.
@QString defaultStyle = QApplication::style()->metaObject()->className();@
have a look in the StyleSheetEditor constructor.
About Ubuntu 11.04 : I read that they are leaving gtk. Maybe this has something to do with it? Since prior versions of ubuntu didn't have this problem.
Thanks Eddy. I just had a quick look and for my application under Ubuntu 11.04, the following code:
QString defaultStyle = QApplication::style()->metaObject()->className();
qDebug() << "BEFORE:" << defaultStyle;
defaultStyle = regExp.cap(1);
qDebug() << "AFTER:" << defaultStyle;@
bq. BEFORE: "QCleanlooksStyle"
This is already the style that my application uses, so I am not sure it helps...?
Did you try to set it in code using that string eventually? I suppose you did.
The reason why I ask is that you told us before that setting it using code went well.
If it's not, there must be a problem with the them I suppose on your system.
Well, I did try indeed (using app->setStyle(new QCleanlookStyle), even though app->style() would return QCleanLookStyle before I set the 'new' style.
Otherwise, I did say that hard setting the style worked in the sense that the style gets applied, but I also said that if I change the theme, then my application doesn't respond to the change.
As I also said, the Qt examples behave the same as my application (i.e. don't respond to a change in the theme) while Qt Creator works as expected (i.e. responds to a change in the theme).
Sorry it couldn't help you out. It was worth trying I guess.
No worries, and yes always worth trying. I guess that if I was to study Qt Creator's source code carefully, then I would find out how they do it...
Sounds like a good idea. And you could learn other usefull things.
Keep us posted.
Oh, I already have from Qt Creator's source code. :) It's just that I am still struggling about the overall structure of the source code, so I tend to use it as a last resource.