×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Applying a Qt style sheet to an application via QApplication

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Remove "standard" preconditions)
hamishwillee (Talk | contribs)
m (Hamishwillee - Addition to article of: Category:MeeGo Category:Symbian. (Add platform categories))
Line 77: Line 77:
 
* [http://doc.trolltech.com/4.7/qapplication.html#qApp qApp macro]
 
* [http://doc.trolltech.com/4.7/qapplication.html#qApp qApp macro]
 
* [http://doc.trolltech.com/4.7/stylesheet.html Qt style sheet documentation]
 
* [http://doc.trolltech.com/4.7/stylesheet.html Qt style sheet documentation]
* [[CS001501 - Creating a gradient background for a QPushButton with style sheet]]
+
* [[CS001501 - Creating a gradient background for a QPushButton with style sheet]][[Category:MeeGo]] [[Category:Symbian]]

Revision as of 09:49, 15 February 2012

Template:KBCS

Article Metadata
Tested with
SDK: Qt 4.5 Garden Tech-preview
Devices(s): Nokia N97
Compatibility
Platform(s): Qt
Symbian
Article
Keywords: Qt, UI, style sheet
Created: taaidant (24 Nov 2009)
Last edited: hamishwillee (15 Feb 2012)

Contents

Overview

This example shows you how to apply a style sheet application-wide.

Preconditions

Applying a Qt style sheet to an application via QApplication

#include "yourwindow.h"
 
#include <QtGui>
#include <QApplication>
 
#include <QFile>
#include <QString>
#include <QTextStream>
 
void loadStyleSheet() {
/* Let's use QFile and point to a resource... */
QFile data(":/style.qss");
QString style;
/* ...to open the file */
if(data.open(QFile::ReadOnly)) {
/* QTextStream... */
QTextStream styleIn(&data);
/* ...read file to a string. */
style = styleIn.readAll();
data.close();
/* We'll use qApp macro to get the QApplication pointer
* and set the style sheet application wide. */

qApp->setStyleSheet(style);
}
}
 
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
YourWindow* w = new YourWindow();
/* Load style sheet */
loadStyleSheet();
w->showMaximized();
int returnValue = a.exec();
w->deleteLater();
return returnValue;
}

Postconditions

Now you can style your application with Qt style sheets.

See also

182 page views in the last 30 days.