PDA

View Full Version : Setting oxygen style on Qt applications



KineticArc
15th October 2010, 16:23
This isn't a question, but more of a how-to that is probably pretty well known. However, I thought I would post something for if someone was looking for a way to set the "oxygen" style (aka KDE 4 style) on their Qt application.

I was having problems viewing the oxygen style on my Windows Xming session, as it was defaulting to Bespin (yuck!). Clean-looks and Plastique were OK, but wanted the native oxygen look I was getting on my virtual machine. So this is what I came up with. I'm sure there might be another way around this, but it was easy enough for me to post it.

I did NOT have an oxygen library in the $QTDIR/plugins/styles directory. I only had libbespin.so. However, since I am running KDE 4.4.5 on a Fedora 13 virtual box, I knew the oxygen style had to be somewhere. So after some searching, I found the oxygen library at /usr/lib/kde4/plugins/styles/oxygen.so. All I did to get the oxygen style on my Qt 4.6.3 application was to open up my CMakeLists.txt file and add:



TARGET_LINK_LIBRARIES(qttest ${QT_LIBRARIES} /usr/lib/kde4/plugins/styles/oxygen.so)


This allowed me to link the oxygen shared object into my code. If you are using a straight g++ command-line, then you can do the "-l/usr/lib/kde4/plugins/styles/oxygen.so". Unfortunately, you are hard-coding this into the make system, but I wasn't looking at portability, but strictly for development purposes. At any rate, that is for another discussion.

I then edited my main.cpp file (or wherever you are performing a "QApplication app(argc, argv)") and did this to it:



#include <QApplication>
#include <QStyleFactory>

int main(int argc, char **argv)
{
QApplication app(argc, argv);
QStyle *style = QStyleFactory::create("oxygen");
if(style)
{
qDebug("Oxygen loaded!"); /// @note You can leave this out if you want!
QApplication::setStyle(style);
}

try
{
Anzo *anzo = new Anzo();
anzo->show();
return app.exec();
}
catch(std::exception &e)
{
std::cout<<"Exception: "<<e.what()<<std::endl;
}

return -1;
}


Note that the above code will not compile for you, but if you only do the QStyle portion, you should load oxygen in.

I might be missing something, but this was a quick and dirty way for me to load the oxygen style into my Qt application.

Thanks!

dfaure
7th January 2013, 15:02
This is very hackish and non portable (not only the path, but also the whole idea of linking to a plugin, you're not supposed to do that).

QStyleFactory::create("oxygen") will find the oxygen plugin if you tell Qt where to look:
export QT_PLUGIN_PATH=/usr/lib/kde4/plugins:$QT_PLUGIN_PATH