With NodeGui, you can style a widget to your needs. If you are familiar with CSS in the web world you would feel right at home. All widgets have a method
setInlineStyle for setting inline styles for the respective widget. The style names and values usually match how CSS works on the web.
Here's an example:
NodeGui makes use of Qt's stylesheet for styling. Qt Style Sheet terminology and syntactic rules are almost identical to those of HTML CSS. Additionally, NodeGui adds support for layout using flex properties like align-items, justify-content, etc. Flexbox layout support is added using facebook's yoga library.
You would write your style properties in a string and pass it to the NodeGui widgets either via global styles or inline styles (similar to how it works in the web).
Lets take a look at an example:
In the case of global stylesheet you can define all your style properties in a stylesheet string and the tell the root view or window to set it as a stylsheet for it and its child widgets. The only difference here from web is that you can set a stylesheet on a widget at any level in the whole tree of widgets, the stylesheet will affect the widget and its children.
In the above example, in order to reference a widget in a stylesheet we will assign it a
objectName using setObjectName instance method. Think of objectName as something similar to an
id in the case of web. Now using the objectName you could reference the widget in the stylesheet and set style properties on them. Do not worry about the layout stuff that is going on here, that will be covered in the next section.
Global stylesheet really becomes powerful when you use things like pseudo-selectors (hover, checked, etc). It also has helps in implementing cascaded styles which allow you to style a group of widgets at once. We will see more about these features below.
More details on all the features and the syntax can be found here: https://doc.qt.io/qt-5/stylesheet-syntax.html
Lets look at this example again:
In most cases it would be easier to style the widgets inline. NodeGui supports inline styling using
setInlineStyle instance method. Inline styles will only affect the widget to which the style is applied to and is often easier to understand and manage. All properties you use in the global stylesheet are available in inline styles as well.
NodeGui style sheets support all the selectors defined in CSS2. Some examples include:
To see a complete list of selectors see here: https://doc.qt.io/qt-5/stylesheet-syntax.html#selector-types
Like in the web, you can style your widget based on its state. An example would be, you might want the color of the button text to be red when its hovered upon. These are possible with pseudo states. Pseudo-states appear at the end of the selector, with a colon (:) in between.
More details here : https://doc.qt.io/qt-5/stylesheet-syntax.html#pseudo-states
Style sheets can be set on the parent widgets and on child widgets. An arbitrary widget's effective style sheet is obtained by merging the style sheets set on the widget's ancestors (parent, grandparent, etc.).
When conflicts arise, the widget's own inline style sheet is always preferred to any inherited style sheet, irrespective of the specificity of the conflicting rules. Likewise, the parent widget's style sheet is preferred to the grandparent's, etc.
The behaviour is similar to what we see on the web.
For more in depth examples see here: https://doc.qt.io/qt-5/stylesheet-syntax.html#cascading
NodeGui style sheets is a css string.
Here if you look carefully, you would notice that there are some differences in the way we write style properties as compared to web.
The quotes you see around
'100%' are necessary so that Qt doesnt interpret them as numbers.
So the rule of thumb is that any integer based property like margin, border, etc can be written without quotes while any string property, it is better to surround them with quotes. PS: Qt does recognise some string based properties without quotes also.
Since we are not running inside a web browser, there are few differences in the properties you could use in NodeGui vs in web.
The complete list is detailed here: https://doc.qt.io/qt-5/stylesheet-reference.html#list-of-properties
Apart from the properties listed in the link, NodeGui also supports layout properties related to Flex. You can use all flex properties such as align-items, justify-content, flex, etc on all widgets. The layout styling will be converted in more detail in the section: Layout.
Advanced usage (Setting QObject Properties)
In Qt, every widget has certain properties set on them using something called as Q_PROPERTY. There are many q-properties that are defined on each widget already. You can also define custom qproperties in the native C++ code yourself too. What does it have to do with styling ? The thing is some of these properties can be altered using qt stylesheet. In Qt's terminology, these properties are called designable properties.
You can discover these properties by following Qt's documentation or by running a simple google search like "center text in QLabel using stylesheet in Qt". These are advanced properties and in practice will come in use rarely but its good to know.
In this section, we mostly covered the paint properties in the NodeGui stylesheet. The next section would cover on how you can use flex to layout your widgets with stylesheet.