1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
Tutorial
========
Creating a StyleSheet
+++++++++++++++++++++
To create a stylesheet, create a :class:`qstylizer.style.StyleSheet` instance.
.. code-block:: python
>>> import qstylizer.style
>>> css = qstylizer.style.StyleSheet()
To add global properties, simply start setting values for any attribute.
.. code-block:: python
>>> css.color.setValue("green")
>>> css.border.setValue("1px solid red")
>>> print(css.toString())
color: green;
border: 1px solid red;
Here is how to create a style rule for `QTabBar::close-button` and set the
`background` property to `transparent`:
.. code-block:: python
>>> css.QTabBar.closeButton.background.setValue("transparent")
>>> print(css.toString())
* {
color: green;
border: 1px solid red;
}
QTabBar::close-button {
background: transparent;
}
There are actually multiple ways to set a property value. All of the following
statements below are equivalent and valid. Take your pick.
.. code-block:: python
>>> css.QTabBar.closeButton.background.setValue("transparent")
>>> css["QTabBar"].closeButton.background.setValue("transparent")
>>> css["QTabBar"]["close-button"].background.setValue("transparent")
>>> css["QTabBar"]["close-button"]["background"].setValue("transparent")
>>> css["QTabBar"]["::close-button"]["background"].setValue("transparent")
>>> css["QTabBar::close-button"].background.setValue("transparent")
>>> css["QTabBar::close-button"]["background"].setValue("transparent")
Global scope vs "* {}"
++++++++++++++++++++++
Adding a sub-style rule will result in a different syntax for the global
property values:
.. code-block:: python
>>> css.color.setValue("green")
>>> css.border.setValue("1px solid red")
>>> print(css.toString())
color: green;
border: 1px solid red;
>>> css.QWidget.backgroundColor.setValue("blue")
>>> print(css.toString())
* {
color: green;
border: 1px solid red;
}
QWidget {
background-color: blue;
}
Unknown Property Names
++++++++++++++++++++++
Any name can be used as an attribute with the use of strings and brackets.
.. code-block:: python
>>> css = qstylizer.style.StyleSheet()
>>> css["QUnknownClass::unknown-subcontrol"]["unknown-prop"].setValue("none")
>>> print(css.toString())
QUnknownClass::unknown-subcontrol {
unknown-prop: none;
}
Not Operator (!)
++++++++++++++++
Here is an example of how to use the `!` operator:
.. code-block:: python
css.QTabBar["!focus"].background.setValue("none")
Object Property
+++++++++++++++
Here is an example of how to set an object property style rule:
.. code-block:: python
css['QLineEdit[echoMode="2"]'].background.setValue("none")
Parser
++++++
An existing stylesheet can be converted to a StyleSheet instance as a starting
point. This is handy if you need to change property values in an existing
template stylesheet.
.. code-block:: python
>>> import qstylizer.parser
>>> stylesheet = """
... QTabBar {
... border-radius: 3px;
... background-color: green;
... }
... QTabBar:focus {
... border: 0px transparent black;
... background-color: red;
... }
... QTabBar::close-button {
... background: transparent;
... }
... """
>>> css = qstylizer.parser.parse(stylesheet)
>>> print(css.QTabBar.focus.toString())
QTabBar:focus {
border: 0px transparent black;
background-color: red;
}
String Output
+++++++++++++
The :meth:`qstylizer.style.StyleRule.toString()` function call with no
parameters will just output the property:values of that style rule in css
format. The *qstylizer.style.StyleRule.toString(recursive=True)* function call
will output the style rule and all of the sub-style rules in its hierarchy.
.. code-block:: python
>>> print(css.QTabBar.toString())
QTabBar {
border-radius: 3px;
background-color: green;
}
>>> print(css.QTabBar.toString(recursive=True))
QTabBar {
border-radius: 3px;
background-color: green;
}
QTabBar:focus {
border: 0px transparent black;
background-color: red;
}
QTabBar::close-button {
background: transparent;
}
|