Archived:Applying a Qt style sheet to an application via QApplication
hamishwillee
(Talk | contribs) m (Text replace - "Category:MeeGo" to "Category:MeeGo Harmattan") |
hamishwillee
(Talk | contribs) m (Text replace - "<code cpp>" to "<code cpp-qt>") |
||
| Line 36: | Line 36: | ||
== Applying a Qt style sheet to an application via QApplication == | == Applying a Qt style sheet to an application via QApplication == | ||
| − | <code cpp> | + | <code cpp-qt> |
#include "yourwindow.h" | #include "yourwindow.h" | ||
Latest revision as of 04:13, 11 October 2012
Archived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (using C++ for the Qt app UI) is deprecated.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (using C++ for the Qt app UI) is deprecated.
Article Metadata
Tested with
SDK: Qt 4.5 Garden Tech-preview
Devices(s): Nokia N97
Compatibility
Platform(s): Qt
Article
Keywords: Qt, UI, style sheet
Created: taaidant
(24 Nov 2009)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Overview
This example shows you how to apply a style sheet application-wide.
Preconditions
- You know how to Using resources in Qt.
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.

