Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
righttoleft.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2021 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page qtquick-positioning-righttoleft.html
6
\title Right-to-left User Interfaces
7
\brief switching text flow and layout.
8
9
\section1 Overview
10
11
This chapter discusses different approaches and options available for implementing right-to-left
12
language support for Qt Quick applications. Some common right-to-left languages include Arabic, Hebrew,
13
Persian and Urdu. Most changes include making sure that text translated to right-to-left languages
14
is properly aligned to the right, and horizontally ordered content in views, lists and grids flows
15
correctly from the right to left.
16
17
In right-to-left language speaking cultures, people naturally scan and read graphic elements and text
18
from the right to left. The general rule of thumb is that content (like photos, videos and maps) is not
19
mirrored, but positioning of the content (like application layouts and the flow of visual elements) is
20
mirrored. For example, photos shown in chronological order should flow from right to left, the
21
low end range of the horizontal sliders should be located at the right side of the slider, and
22
text lines should be aligned to the right side of the available text area. The location of visual
23
elements should not be mirrored when the position is related to a content; for example, when a
24
position marker is shown to indicate a location on a map. Also, there are some special cases you may
25
need to take into account where right-to-left language speakers are used to left-to-right
26
positioning, for example when using number dialers in phones and media play, pause, rewind and
27
forward buttons in music players.
28
29
\section1 Text Alignment
30
31
(This applies to the \l Text, \l TextInput and \l TextEdit types.)
32
33
When the horizontal alignment of a text item is not explicitly set, the text element is
34
automatically aligned to the natural reading direction of the text. By default left-to-right text
35
like English is aligned to the left side of the text area, and right-to-left text like Arabic is
36
aligned to the right side of the text area. The alignment of a text element with empty text takes
37
its alignment cue from \l QInputMethod::inputDirection(), which is based on the active
38
system locale.
39
40
This default locale-based alignment can be overridden by setting the \c horizontalAlignment
41
property for the text element, or by enabling layout mirroring using the \l LayoutMirroring attached
42
property, which causes any explicit left and right horizontal alignments to be mirrored.
43
Note that when \l LayoutMirroring is set, the \c horizontalAlignment property value remains unchanged;
44
the effective alignment of the text element that takes the mirroring into account can be read from the
45
\c effectiveHorizontalAlignment property.
46
47
\snippet qml/righttoleft.qml 0
48
49
\section1 Layout Direction of Positioners and Views
50
51
(This applies to the \l Row, \l Grid, \l Flow, \l ListView and \l GridView types.)
52
53
Types used for horizontal positioning and model views have the \c layoutDirection
54
property for controlling the horizontal direction of the layouts. Setting \c layoutDirection to
55
\c Qt.RightToLeft causes items to be laid out from the right to left. By default Qt Quick follows
56
the left-to-right layout direction.
57
58
The horizontal layout direction can also be reversed through the \l LayoutMirroring attached property.
59
This causes the effective \c layoutDirection of positioners and views to be mirrored. Note the actual value
60
of the \c layoutDirection property will remain unchanged; the effective layout direction of positioners and
61
views that takes the mirroring into account can be read from the \c effectiveLayoutDirection property.
62
63
\snippet qml/righttoleft.qml 1
64
65
\section1 Layout Mirroring
66
67
The attached property \l LayoutMirroring is provided as a convenience for easily implementing right-to-left
68
support for existing left-to-right Qt Quick applications. It mirrors the behavior of \l {anchor-layout}
69
{Item anchors}, the layout direction of \l{Item Positioners}{positioners} and
70
\l{qtquick-modelviewsdata-modelview.html}{model views}, and the explicit text alignment of QML text types.
71
72
You can enable layout mirroring for a particular \l Item:
73
74
\snippet qml/righttoleft.qml 2
75
76
Or set all child types to also inherit the layout direction:
77
78
\snippet qml/righttoleft.qml 3
79
80
Applying mirroring in this manner does not change the actual value of the relevant anchor,
81
\c layoutDirection or \c horizontalAlignment properties. The separate read-only property
82
\c effectiveLayoutDirection can be used to query the effective layout
83
direction of positioners and model views that takes the mirroring into account. Similarly the \l Text,
84
\l TextInput and \l TextEdit types have gained the read-only property \c effectiveHorizontalAlignment
85
for querying the effective visual alignment of text.
86
87
Note that application layouts and animations that are defined using \l {Item::}{x} property values (as
88
opposed to anchors or positioner types) are not affected by the \l LayoutMirroring attached property.
89
Therefore, adding right-to-left support to these types of layouts may require some code changes to your application,
90
especially in views that rely on both the anchors and x coordinate-based positioning. Here is one way to use
91
the \l LayoutMirroring attached property to apply mirroring to an item that is positioned using \l {Item::}{x}
92
coordinates:
93
94
\snippet qml/righttoleft.qml 4
95
96
Not all layouts should necessarily be mirrored. There are cases where a visual type is positioned to
97
the right side of the screen for improved one-handed use, because most people are right-handed, and not
98
because of the reading direction. In the case that a child type should not be affected by mirroring,
99
set the \l {LayoutMirroring::enabled}{LayoutMirroring.enabled} property for that type to false.
100
101
Qt Quick is designed for developing animated, fluid user interfaces. When mirroring your application, remember to test that
102
the animations and transitions continue to work as expected. If you do not have the resources to add
103
right-to-left support for your application, it may be better to just keep the application layouts left
104
aligned and just make sure that text is translated and aligned properly.
105
106
\section1 Mirroring Icons
107
108
(This applies to \l Image, \l BorderImage and \l AnimatedImage types.)
109
110
Most images do not need to be mirrored, but some directional icons, such as arrows, may need to be mirrored.
111
The painting of these icons can be mirrored with a dedicated \c mirror property:
112
113
\snippet qml/righttoleft.qml 5
114
115
\section1 Default Layout Direction
116
117
Use the \l {QtQml::Qt::application}{Qt.application.layoutDirection} property
118
to query the active layout direction of the application. It inherits
119
QGuiApplication::layoutDirection(), which determines the layout direction from
120
the active language translation file.
121
122
To define the layout direction for a particular locale, declare the dedicated
123
string literal \c QT_LAYOUT_DIRECTION in the context \c QGuiApplication as
124
either \c LTR or \c RTL.
125
126
First, introduce this line somewhere in your QML source code:
127
128
\code
129
qsTr("QT_LAYOUT_DIRECTION","QGuiApplication");
130
\endcode
131
132
Then use \l {Using lupdate} to generate the translation source file.
133
134
This appends the following declaration to the translation file, where you can
135
enter either \c LTR or \c RTL as the translation for the locale.
136
137
\code
138
<context>
139
<name>QGuiApplication</name>
140
<message>
141
<location filename="myapp.qml" line="33"/>
142
<source>QT_LAYOUT_DIRECTION</source>
143
<translation type="unfinished">RTL</translation>
144
</message>
145
</context>
146
\endcode
147
148
Next, add the following bindings to the root QML component of your application:
149
\code
150
LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft
151
LayoutMirroring.childrenInherit: true
152
\endcode
153
154
The first binding ensures that the UI will be mirrored appropriately when a
155
right-to-left locale is set. The second binding ensures that child items of the
156
root component will also respect mirroring.
157
158
You can test that the layout direction works as expected by running your Qt Quick application with
159
the compiled translation file:
160
161
\code
162
qml myapp.qml -translation myapp.qm
163
\endcode
164
165
You can test your application in right-to-left layout direction by calling the
166
static function \l QGuiApplication::setLayoutDirection():
167
168
\code
169
QGuiApplication app(argc, argv);
170
app.setLayoutDirection(Qt::RightToLeft);
171
\endcode
172
173
*/
qtdeclarative
src
quick
doc
src
concepts
positioning
righttoleft.qdoc
Generated on
for Qt by
1.16.1