📜 ⬆️ ⬇️

Debugging Qt Style Sheet

The popular Qt framework has a very convenient UI style management mechanism - Qt Style Sheet. Thanks to which the style of widgets and windows can be set in a CSS-like form. The style can be stored both in the application resources and in the external file.
In my practice I was constantly confronted with the task of debugging a style file in a real project. If for web applications it is enough to press F5 in the browser, then on the desktop you will have to restart the application, sometimes log in, get to the desired form. This is a big waste of time. Let's try to make a tool for easy debugging styles. Formulate a custom script:

We want to edit the style file and immediately see how it looks in any form of the application.


Starting implementation, we will assume that the user will be satisfied with the option - save the style file - click the update button in the application. The implementation will rely on the application's event capture mechanism (eventFilter). When the application starts, we will install an event interceptor that will reload the style file when the specified key is pressed.
')
A bit of code:
Class description
StyleLoader.h
#ifndef STYLELOADER_H #define STYLELOADER_H #include <QObject> #include <QKeySequence> class StyleLoader: public QObject { Q_OBJECT public: static void attach(const QString& filename = defaultStyleFile(), QKeySequence key = QKeySequence("F5")); bool eventFilter(QObject *obj, QEvent *event); private: StyleLoader(QObject * parent, const QString& filename, const QKeySequence& key); void setAppStyleSheet(); static QString defaultStyleFile(); QString m_filename; QKeySequence m_key; }; #endif // STYLELOADER_H 


and implementation:
 #include "StyleLoader.h" #include <QApplication> #include <QFile> #include <QKeyEvent> #include <QDebug> void StyleLoader::attach(const QString &filename, QKeySequence key) { StyleLoader * loader = new StyleLoader(qApp, filename, key); qApp->installEventFilter(loader); loader->setAppStyleSheet(); } bool StyleLoader::eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); if(m_key == QKeySequence(keyEvent->key())) setAppStyleSheet(); return true; } else return QObject::eventFilter(obj, event); } void StyleLoader::setAppStyleSheet() { QFile file(m_filename); if(!file.open(QIODevice::ReadOnly)) { qDebug() << "Cannot open stylesheet file " << m_filename; return; } QString stylesheet = QString::fromUtf8(file.readAll()); qApp->setStyleSheet(stylesheet); } QString StyleLoader::defaultStyleFile() { return QApplication::applicationDirPath() + "/style.qss"; } StyleLoader::StyleLoader(QObject *parent, const QString& filename, const QKeySequence &key): QObject(parent), m_filename(filename), m_key(key) { } 



To connect the tool to the application, it is enough to write one line somewhere in main ():

 StyleLoader::attach(); 

In this version, the default settings will be used:
Style file: __ /style.qss ;
Update key: F5 .

You can set your own values:
 StyleLoader::attach("c:/myStyle.qss", QKeySequence("F6")); 


Now we can start our application, at any time correct the style file, press F5 and immediately see how it will look.

PS: the code is compact, so I recommend just to drag yourself into the project. Soon lay out on Github under a free license.

Source: https://habr.com/ru/post/232323/


All Articles