PDA

View Full Version : Qt style sheets



locus
3rd April 2007, 19:54
I am trying to implement a feature in y application where the user can change the face of the GUI by choosing from a list of styles.

Each option should apply a different style sheet to the application.

Questions:

Can i arbitrarily just add styles to application while it runs, as described above?

and more importatntly

Could anyone give me some pointers on how to go about saving the style settings of the application.

I am already using QSettings to save the geometry and position of the main window, along with some other options, but i don't see a way to save the style sheet options yet.

all sugstions welcomed.

Thanks for your response.

patrik08
3rd April 2007, 21:03
I am trying to implement a feature in y application where the user can change the face of the GUI by choosing from a list of styles.

Each option should apply a different style sheet to the application.

Questions:

Can i arbitrarily just add styles to application while it runs, as described above?

and more importatntly

Could anyone give me some pointers on how to go about saving the style settings of the application.

I am already using QSettings to save the geometry and position of the main window, along with some other options, but i don't see a way to save the style sheet options yet.

all sugstions welcomed.

Thanks for your response.



just tested tody on window && ubuntu linux ...

on main.cpp to

QSettings setter;
QString soko = QString(setter.value("WinStyle").toString());
and load the style ...





#include "arthurstyle.h" /* not work on QWorkspace */
#include "arthurwidgets.h"
#include "norwegianwoodstyle.h"
#if defined Q_WS_WIN
#include <QWindowsXPStyle>
#include <QWindowsVistaStyle>
#endif

void Gui_Main::ModStyle()
{
QStringList items = QStyleFactory::keys(); /* incomming list supported on all os */
items.append("QWindowsVistaStyle");
#ifdef QT_OPENGL_SUPPORT
items.append("ArthurStyle");
#endif
items.append("NorwegianWood");
bool ok = false;
QString soko = QInputDialog::getItem(0,"Select style name.",
"Style:", items, 0, false, &ok);

if (ok) {
setter.setValue("WinStyle",soko);

if (soko.size() > 0 ) {
if (soko == "NorwegianWood") {
QApplication::setStyle(new NorwegianWoodStyle);
} else if (soko == "QWindowsVistaStyle") {
#if defined Q_WS_WIN
QApplication::setStyle(new QWindowsVistaStyle);
#endif
} else if (soko == "ArthurStyle") {
#ifdef QT_OPENGL_SUPPORT
QStyle *arthurStyle = new ArthurStyle();
QList<QWidget *> widgets = qFindChildren<QWidget *>(this);
foreach (QWidget *w, widgets) {
w->setStyle(arthurStyle);
}
QApplication::setStyle(arthurStyle);
#endif
} else {
QApplication::setStyle(QStyleFactory::create(soko) );
}
///////////QApplication::setPalette(QApplication::style()->standardPalette());
}
}
}

wysota
3rd April 2007, 21:15
You may apply stylesheets whenever you want.

elcuco
3rd April 2007, 22:03
to each widget? or globally to the whole application?

patrik08
4th April 2007, 01:36
to each widget? or globally to the whole application?

QApplication is the top class ...
after the command ....
QApplication::setStyle(arthurStylexxxxx);
the style is visible....

i save to qsetting and edit at mainwindow...


on main..... load the style and all other xxx



int main(int argc, char *argv[]) {

QApplication a( argc, argv );
QCoreApplication::setOrganizationName(_ORGANIZATIO N_NAME_);
QCoreApplication::setOrganizationDomain(_PROGRAM_N AME_DOMAINE_);
QCoreApplication::setApplicationName(_PROGRAM_NAME _);

QString localedirfile;

#if defined Q_WS_MAC
localedirfile = QString("%1/locale/edit_%2.qm").arg(WORK_CACHEDIR).arg(UserLanguage());
#endif
#if defined Q_WS_WIN
localedirfile = QString("%1/locale/edit_%2.qm").arg(QCoreApplication::applicationDirPath()).arg( UserLanguage());
#endif
#if defined Q_WS_X11
localedirfile = QString("%1/locale/edit_%2.qm").arg(WORK_CACHEDIR).arg(UserLanguage());
#endif
QTranslator translator;
translator.load(localedirfile);
a.installTranslator(&translator);

if (!NetworkEnable()) {
QMessageBox::warning(0,"NetworkInterface","Check Your Network! SQL DB get data........");
return 0;
}

Preload *splash = new Preload(1400,LICENCE); /* animated painter dialog widget loading msec */
splash->exec();

QSettings setter;
QString soko = QString(setter.value("WinStyle").toString());
Gui_Main::self()->setWindowTitle( _PROGRAM_TITLE_ );

if (soko.size() > 0 ) {
if (soko == "NorwegianWood") {

a.setStyle(new NorwegianWoodStyle);

} else if (soko == "ArthurStyle") {
#ifdef QT_OPENGL_SUPPORT
QStyle *arthurStyle = new ArthurStyle();
QList<QWidget *> widgets = qFindChildren<QWidget *>(Gui_Main::self());
foreach (QWidget *w, widgets) {
w->setStyle(arthurStyle);
}
a.setStyle(arthurStyle);
#endif
} else if (soko == "QWindowsVistaStyle") {

#if defined Q_WS_WIN
a.setStyle(new QWindowsVistaStyle);
#endif

} else {
a.setStyle(QStyleFactory::create(soko));
}

///////////a.setPalette(QApplication::style()->standardPalette());
}
QString argument_1;
argument_1 = QString::fromLatin1(argv[1]);
if (argument_1.contains(".chre")) {
Gui_Main::self()->setOpen(QUrl(argument_1));
}
Gui_Main::self()->show();
///////// splash self close /////
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
};

Eldritch
5th April 2007, 17:03
to each widget? or globally to the whole application?

In the Overview section of the QStyleSheet (http://doc.trolltech.com/4.2/stylesheet.html) docs, it's pretty clear that at any point in a widget hierarchy, you can call QWidget::setStyleSheet(). So, for that widget and its children, the style applies. If you modify the styles on a child of widget A, say, in A', then A' and its children get the new style, etc.

Hope this helps.