Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Dynamic Properties and Stylesheets: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(+link to official docs)
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
'''English''' [[DynamicPropertiesAndStylesheets Spanish|Spanish]] [[DynamicPropertiesAndStylesheets German|German]]
{{LangSwitch}}
[[Category:HowTo]]


=Dynamic Properties and Stylesheets=


Stylesheets makes it possible to customize the look of Qt applications without having to master the magic behind Qt styles. For lighter tweaks, to the quite complex, stylesheets can do the job. For the real unique user experience, QtQuick and <code>QGraphicsView</code> is a better choice.
:''See also the official documentation: {{DocLink|stylesheet-examples|customizing-using-dynamic-properties|Customizing Using Dynamic Properties}}''
== Introduction ==


When using stylesheets, styles are applied using selectors. For instance, the snippet below gives all <code>QLineEdit</code> widgets a yellow background color, if this style sheet is set on the form widget. Here, the selector is the word ''QLineEdit''.
Qt stylesheets make it possible to customize the look of Qt applications without having to master the magic behind Qt styles. From lighter tweaks to the quite complex, stylesheets can do the job. For a fully customized and unique user experience, QtQuick and {{DocLink|QGraphicsView}} are a better choice.


There are numerous selectors based on object names, sub-controls, pseudo-states and more. These can be used to change the look of a user interface and also make all <code>QPushButton</code> widgets named ''okButton'' have green text, and so on.
When using stylesheets, styles are applied using selectors. For instance, the snippet below gives all {{DocLink|QLineEdit}} widgets a yellow background color, if this style sheet is set on the form widget or on a widget that contains it. Here, the selector is the word <tt>QLineEdit</tt>:


For dynamic changes in the user interface look, the property value selector can be used in combination with dynamic properties. Dynamic properties was introduced in Qt 4.2 and allows your to assign property values to <code>QObject</code>s for properties that do not exist at compile time. I.e., if you choose to set the a property called ''urgent'' to true for a <code>QObject</code>, that property will stick even tough the class does not contain a <code>Q_PROPERTY</code> macro for the property ''urgent''.
<code>
QLineEdit
{
  background: yellow;
}
</code>


Creating a stylesheet selector relying on a dynamic property, e.g. ''urgent'', makes it possible to highlight parts of the user interface in a very dynamic manner. For instance, adding the following rule to the stylesheet above will given any <code>QLineEdit</code> with the ''urgent'' property set to true red text on a yellow background.
There are numerous selectors based on object names, sub-controls, pseudo-states and more. These can be used to change the look of a user interface and also make all {{DocLink|QPushButton}} widgets named ''okButton'' have green text, and so on.


This does not have to be limited to <code>QLineEdits</code> though. Setting the ''urgent'' property to true on a <code>QCheckBox</code> or <code>QPushButton</code> will make their texts red as well. Basically, any stylesheet aware widget can be turned red using this simple trick.
== Custom properties ==


==Limitations==
For dynamic changes in the user interface look, the property value selector can be used in combination with dynamic properties. Dynamic properties were introduced in Qt 4.2 and allow you to assign property values to {{DocLink|QObject}}s for properties that do not exist at compile time. I.e. if you choose to set the a property called ''urgent'' to true for a {{DocLink|QObject}}, that property will stick even tough the class does not contain a <tt>Q_PROPERTY</tt> macro for the property ''urgent''.


There are however limitations to using this trick. The main one is, that the style will not update itself automatically when the value of the property changes. This limits the usefulness of this feature, as you can not simply trigger an update of the visual appearance of a widget by only changing a simple property that you referenced from the style sheet.
Creating a stylesheet selector relying on a dynamic property, e.g. ''urgent'', makes it possible to highlight parts of the user interface in a very dynamic manner. For instance, adding the following rule to the stylesheet above will given any {{DocLink|QLineEdit}} with the ''urgent'' property set to true red text on a yellow background.


'''''Note :-'''''
<code>
QLineEdit[urgent=true] {
  color: red;
}
</code>


While implementing dynamic properties we need to update the widget when the property changes , For eg we want to paint a frame with red color if there is an error. So we can set the stylesheet for the frame as
This does not have to be limited to QLineEdits though. if the selector was changed from <tt>QLineEdit[urgent=true]</tt> to <tt>[urgent=true]</tt>, setting the ''urgent'' property to true on a {{DocLink|QCheckBox}} or QPushButton will make their texts show in red as well. Basically, any stylesheet aware widget could be turned red using this generic selector.


Then wherever we are changing the property we need to add the following code to trigger an update
== Limitations ==


For the above tstFrame we have set the objectName as “TestDisplayFrame”.
There are limitations to using this trick. The main one is that the style will not update itself automatically when the value of a property referenced from the style sheet changes. Instead, you must manually trigger an update of the visual appearance of a widget when a styled property changes. For example:


===Categories:===
<code>
myLineEdit->setProperty("urgent", true);
myLineEdit->style()->unpolish(myLineEdit);
myLineEdit->style()->polish(myLineEdit);
</code>


* [[:Category:HowTo|HowTo]]
Note that this must be done in the widget to which the style was applied. {{DocLink|qstyle|polish|QStyle::polish}} accepts either a QWidget or a QApplication as a parameter.
* [[:Category:Learning|Learning]]

Latest revision as of 11:40, 20 June 2016

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh


See also the official documentation: Customizing Using Dynamic Properties

Introduction

Qt stylesheets make it possible to customize the look of Qt applications without having to master the magic behind Qt styles. From lighter tweaks to the quite complex, stylesheets can do the job. For a fully customized and unique user experience, QtQuick and QGraphicsView are a better choice.

When using stylesheets, styles are applied using selectors. For instance, the snippet below gives all QLineEdit widgets a yellow background color, if this style sheet is set on the form widget or on a widget that contains it. Here, the selector is the word QLineEdit:

QLineEdit
{
  background: yellow;
}

There are numerous selectors based on object names, sub-controls, pseudo-states and more. These can be used to change the look of a user interface and also make all QPushButton widgets named okButton have green text, and so on.

Custom properties

For dynamic changes in the user interface look, the property value selector can be used in combination with dynamic properties. Dynamic properties were introduced in Qt 4.2 and allow you to assign property values to QObjects for properties that do not exist at compile time. I.e. if you choose to set the a property called urgent to true for a QObject, that property will stick even tough the class does not contain a Q_PROPERTY macro for the property urgent.

Creating a stylesheet selector relying on a dynamic property, e.g. urgent, makes it possible to highlight parts of the user interface in a very dynamic manner. For instance, adding the following rule to the stylesheet above will given any QLineEdit with the urgent property set to true red text on a yellow background.

QLineEdit[urgent=true] {
  color: red;
}

This does not have to be limited to QLineEdits though. if the selector was changed from QLineEdit[urgent=true] to [urgent=true], setting the urgent property to true on a QCheckBox or QPushButton will make their texts show in red as well. Basically, any stylesheet aware widget could be turned red using this generic selector.

Limitations

There are limitations to using this trick. The main one is that the style will not update itself automatically when the value of a property referenced from the style sheet changes. Instead, you must manually trigger an update of the visual appearance of a widget when a styled property changes. For example:

myLineEdit->setProperty("urgent", true);
myLineEdit->style()->unpolish(myLineEdit);
myLineEdit->style()->polish(myLineEdit);

Note that this must be done in the widget to which the style was applied. QStyle::polish accepts either a QWidget or a QApplication as a parameter.