Required Properties
Sometimes, your component needs to set some properties, but there are no appropriate default values. For example, you may be concerned about the accessibility of a button, so when you create an AccessibleButton control, it should have at least one description property.


 

However, the fact that a button has a description attribute does not mean that anyone will set it. So at some point, you or your colleagues may instantiate the component using the following code:


That's all for accessibility: now the description property is just an empty string! Of course, you can set a default value for the attribute, but which one to use? "Button" is basically useless. You shouldn't have heard this? Okay, at least QA may focus on it now. However, wouldn't it be more useful if the QML engine knew that this attribute needed to be set?
Unfortunately, it was not possible to enforce the description property before Qt 5.15. But starting from Qt 5.15, this became possible:



 


Now, if you create an AccessibleButton without setting the description property, the entire application will not be able to start. But if the component is dynamically created (such as loaded through a Loader), this cannot be achieved. In this case, only runtime warnings will appear.
We also plan to add more tool support for qmlint and QtCreator to display warnings when Required Properties are not set.
Required Properties and Delegates
In addition, Required Properties play a special role in Delegates. As you may know, Delegates can directly access the provided model roles by name and other properties such as model and index.



 


If your Delegates do not include Required Properties, no changes will occur here. However, if they contain at least one Required Property, then these names cannot be accessed anymore. On the contrary, you must explicitly specify them as Required Properties.


 


Then the QML engine will set the required properties accordingly. Please note that if your model is editable, there is an important difference between the new and old methods: using the old method, you can write the code as follows:


The model will also be updated accordingly. But if you write code like this

 


Then, the binding of the description will be broken (the QML engine will print a warning), and the model will not be updated. We have decided to adopt this behavior to ensure that the behavior of components does not differ significantly, whether used within or outside of delegates. In addition, we do not encourage anyone to perform imperative assignments on properties (as this often breaks the binding).


If you really want to update the value of the model, of course, there is another way to do it: set the model to Required Properties and use the following code



We recommend that you always use Required Properties in Delegates. This avoids unrestricted searching, which is a problem for tools and often slows down processing speed.


Inline Components


Another new feature in Qt 5.15 is inline components. As the name suggests, they allow you to define a new component in a file. The basic grammar is



 

Within the file, you can refer to new components by name, just as defined in their own file. Let's take the LabeledImage component as an example to illustrate its working principle:


You can also reference this component in other files. In this case, you need to add the name containing its components before its name:


You may wonder, why do we still use inline components when QML already has component types? Looking at the previous example, we can see that inline components allow you to perform the following actions that components cannot perform:

You can create component instances without incurring loader overhead.

You can use component types in property declarations.

You can reference the component in files other than the one that defines it.

I hope you can easily find inline components like us!

Nullish Coalescing

The last new language feature was implemented by our intern Maximilian Goldstein. Although QML typically only supports EcmaScript 6, Maximilian has added support for an upcoming language feature that is being added to the latest EcmaScript standard: null value merging. Referencing MDN:

The null value merge operator (??) is a logical operator that returns its right operand when its left operand is empty or undefined, otherwise it returns its left operand.

For detailed information, please refer to the MDN page. Here is an example of how to use it in QML to set properties in JSON and provide reasonable default values without providing properties.


Note that we cannot use "| |" instead of "?" when setting brightness. Because settings. brightness may already be 0, in this case, we will obtain the default value.