Qml Styling: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(the mentioned bug is already resolved, the import is not necessary now (tested in 5.15))
 
(11 intermediate revisions by 7 users not shown)
Line 1: Line 1:
'''English''' [[QmlStyling Spanish|Spanish]] [[QmlStyling Italian|Italian]] [[QmlStyling Hungarian|Magyar]] [http://qt-devnet.developpez.com/tutoriels/qt-quick/qml/style/ French] ''[qt-devnet.developpez.com]'' [[QmlStyling Bulgarian|Български]]<br />
{{LangSwitch}}


=Styling in <span class="caps">QML</span>=
QML provides several mechanisms for styling a UI. Below are three common approaches.
 
<span class="caps">QML</span> provides several mechanisms for styling a UI. Below are three common approaches.


==Approach 1: Custom Component==
==Approach 1: Custom Component==


<span class="caps">QML</span> supports defining your own [http://doc.qt.io/qt-5/qtqml-referenceexamples-properties-example.html custom components] ''[qt.io]''. Below, we create a custom component '''TitleText''' that we can use whenever our UI requires a title. If we want to change the appearance of all the titles in our UI, we can then just edit ''TitleText.qml'', and the changes will apply wherever it is used.
QML supports defining your own [http://doc.qt.io/qt-5/qtqml-referenceexamples-properties-example.html custom components]. Below, we create a custom component '''TitleText''' that we can use whenever our UI requires a title. If we want to change the appearance of all the titles in our UI, we can then just edit ''TitleText.qml'', and the changes will apply wherever it is used.<syntaxhighlight lang="qml">
// TitleText.qml
Text {
    horizontalAlignment: Text.AlignHCenter
    font.pixelSize: 50
    color: "green"
}
</syntaxhighlight><syntaxhighlight lang="qml">
// In use
TitleText {
    text: "Title 1"
}
</syntaxhighlight>


==Approach 2: Style Singleton==
==Approach 2: Style Singleton==


In this approach we define a '''Style''' <span class="caps">QML</span> singleton object that contains a collection of properties defining the style. As a <span class="caps">QML</span> singleton, a common instance can be access from anywhere which imports this directory. Note that <span class="caps">QML</span> singletons require a qmldir file with the '''singleton''' keyword preceding the Style type declaration; they cannot be picked up implicitly like other types.
In this approach we define a '''Style''' QML singleton object that contains a collection of properties defining the style. As a QML singleton, a common instance can be access from anywhere which imports this directory. Note that QML singletons require a qmldir file with the '''singleton''' keyword preceding the Style type declaration; they cannot be picked up implicitly like other types. When using Qrc resources both the Style.qml and the qmldir file must be included in the .qrc resource file.<syntaxhighlight lang="qml">
// Style.qml
pragma Singleton
import QtQuick 2.0
 
QtObject {
    property int textSize: 20
    property color textColor: "green"
}
</syntaxhighlight><syntaxhighlight lang="qml">
// In use
import QtQuick 2.0
 
Text {
    font.pixelSize: Style.textSize
    color: Style.textColor
    text: "Hello World"
}
</syntaxhighlight>


==Approach 3: Hybrid (Style Singleton + Custom Component)==
==Approach 3: Hybrid (Style Singleton + Custom Component)==


In this approach, we have a '''Style''' singleton that is used by our custom component.
In this approach, we have a '''Style''' singleton that is used by our custom component.<syntaxhighlight lang="qml">
// Style.qml
pragma Singleton
import QtQuick 2.0
 
QtObject {
    property int titleAlignment: Text.AlignHCenter
    property int titleFontSize: 50
    property color titleColor: "green"
}
</syntaxhighlight><syntaxhighlight lang="qml">
// TitleText.qml
import QtQuick 2.0
 
Text {
    horizontalAlignment: Style.titleAlignment
    font.pixelSize: Style.titleFontSize
    color: Style.titleColor
}
</syntaxhighlight><syntaxhighlight lang="qml">
// In use
TitleText {
    text: "Title 1"
}
</syntaxhighlight>


==Nesting QtObjects==
==Nesting QtObjects==


If you need to nest QtObject to access more defined properties (i.e. border.width.normal) you can do the following:
If you need to nest QtObject to access more defined properties (i.e. border.width.normal) you can do the following:<syntaxhighlight lang="qml">
 
// Style.qml
===Categories:===
pragma Singleton


* [[:Category:Developing with Qt|Developing_with_Qt]]
QtObject {
** [[:Category:Developing with Qt::Qt-Quick|Qt Quick]]
    property QtObject window: QtObject {
        property color background: "white"
    }
    property QtObject border: QtObject {
        property QtObject width: QtObject {
            property int normal: 1
            property int big: 3
        }
        property QtObject color: QtObject {
            property color normal: "gray"
            property color focus: "blue"
            property color disabled: "red"
        }
    }
}
</syntaxhighlight>

Latest revision as of 17:28, 5 October 2021

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

QML provides several mechanisms for styling a UI. Below are three common approaches.

Approach 1: Custom Component

QML supports defining your own custom components. Below, we create a custom component TitleText that we can use whenever our UI requires a title. If we want to change the appearance of all the titles in our UI, we can then just edit TitleText.qml, and the changes will apply wherever it is used.

// TitleText.qml
Text {
    horizontalAlignment: Text.AlignHCenter
    font.pixelSize: 50
    color: "green"
}
// In use
TitleText {
    text: "Title 1"
}

Approach 2: Style Singleton

In this approach we define a Style QML singleton object that contains a collection of properties defining the style. As a QML singleton, a common instance can be access from anywhere which imports this directory. Note that QML singletons require a qmldir file with the singleton keyword preceding the Style type declaration; they cannot be picked up implicitly like other types. When using Qrc resources both the Style.qml and the qmldir file must be included in the .qrc resource file.

// Style.qml
pragma Singleton
import QtQuick 2.0

QtObject {
    property int textSize: 20
    property color textColor: "green"
}
// In use
import QtQuick 2.0

Text {
    font.pixelSize: Style.textSize
    color: Style.textColor
    text: "Hello World"
}

Approach 3: Hybrid (Style Singleton + Custom Component)

In this approach, we have a Style singleton that is used by our custom component.

// Style.qml
pragma Singleton
import QtQuick 2.0

QtObject {
    property int titleAlignment: Text.AlignHCenter
    property int titleFontSize: 50
    property color titleColor: "green"
}
// TitleText.qml
import QtQuick 2.0

Text {
    horizontalAlignment: Style.titleAlignment
    font.pixelSize: Style.titleFontSize
    color: Style.titleColor
}
// In use
TitleText {
    text: "Title 1"
}

Nesting QtObjects

If you need to nest QtObject to access more defined properties (i.e. border.width.normal) you can do the following:

// Style.qml
pragma Singleton

QtObject {
    property QtObject window: QtObject {
        property color background: "white"
    }
    property QtObject border: QtObject {
        property QtObject width: QtObject {
            property int normal: 1
            property int big: 3
        }
        property QtObject color: QtObject {
            property color normal: "gray"
            property color focus: "blue"
            property color disabled: "red"
        }
    }
}