PDA

View Full Version : How to get the native QStyle subclass?



Asperamanca
7th October 2011, 13:52
It must be really simple, but I can't figure it out:

How do I create a QStyle object that contains the native look and feel of the computer I'm on?

I basically want to write something like this:
QStyle* pNativeStyle = QStyleFactory::createNativeStyle();
...but this method does not exist.

By the time my code runs, the QApplication style has already been tinkered with, so I can't use QApplication::style().
The QStyleFactory does not seem to have an option to simply create "the native style", whichever subclass of QStyle that is.


How do I do it?

Kind regards

Robert

nish
7th October 2011, 15:24
yes the style has been created but its not used until you allow it to.


main()
{
QApplication app(argc,argv);

QStyle *nativeStyle = app.style();
//do whatever you want to do with style;

//Style would be used now.
QMainWindow mw;
mw.show();

return app.exec();
}

Asperamanca
7th October 2011, 16:29
I'm inside a library that only gets called after the QApplication::style() has already been modified.

wysota
7th October 2011, 17:24
There is no such thing as a "native style". I'm on Linux, what's my "native style"? You can detect the operating system and based on that and Qt's source code you can check which style gets chosen by Qt by default. But there is no guarantee you are going to choose the right style since a particular style might not be available on a particular compilation.

nish
10th October 2011, 09:10
If you want a new unmodified style what the default QApplication has selected on startup, try this.




QStyle *s = app.style();
qDebug() << s->objectName();
QStyle *ns = QStyleFactory::create(s->objectName()); qDebug() << ns << ns->objectName();