Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qtquicklayouts-responsive.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2023 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page qtquicklayouts-responsive.html
6
\title Qt Quick Responsive Layouts
7
\brief A guideline to make Qt Quick Layouts adaptive to screen size.
8
9
Layouts are a good technique to make resizable user interfaces. However,
10
this approach has its limitations as we cannot shrink and expand items
11
limitless without sacrificing usability and aesthetics. At some point, it
12
makes more sense to reorganize, remove or add certain elements. Adapting to
13
different devices (e.g. phones and tables) and screen orientations
14
(landscape or portrait) can be implemented similarly. This is what
15
we usually understand as responsive layouts and \l {Qt Quick Layouts}
16
provide various APIs to implement them.
17
18
\section1 Static hierarchy, adaptive layout
19
20
Layouts have a hierarchy, which is usually defined by the declarative QML
21
code. For some simple responsive layouts, it is sufficient to keep the
22
hierarchy unmodified and instead just tweak some of the properties that
23
affect layouting.
24
25
\section2 Declarative description
26
27
The simplest approach to change layouting is to modify layout properties
28
and \l {Layout} attached properties with small expressions. You can for
29
instance use ternary operators in order to modify the layout depending
30
on its width.
31
\l {Item} properties, such as \l {Item.visible},
32
hiding or showing various parts of the interface, can be modified the same
33
way.
34
35
In the following snippet, this concept is used to change a two-column
36
layout into a single-column layout if the window width is smaller than a
37
certain value.
38
39
\snippet layouts/responsiveDeclarative.qml document
40
41
The resulting layouts look like this, depending on the width of the window.
42
43
\div {class="float-right"}
44
\inlineimage simpleProxy.png
45
{Narrow window with stacked layout and wide window with
46
side-by-side layout}
47
\enddiv
48
49
Various levels of layouts and items can be nested, but each \l Item
50
can only be moved within its \l{Item.parent}{parent}.
51
\section2 States
52
53
The same result can be achieved with \l {Qt Quick States}. The upside of
54
using states is that the \l {Layout} properties for a specific layout are
55
collected at a single point in the QML file (at least the changing ones).
56
The previously shown example can be implemented as follows and the result
57
looks and behaves the exact same.
58
59
\snippet layouts/responsiveStates.qml document
60
61
\section2 LayoutItemProxy
62
63
A third approach is the application of the \l {LayoutItemProxy}. The
64
implementation of the previously shown minimalistic example can be found in
65
the type documentation. In contrast to previously shown solutions, the
66
\l {LayoutItemProxy} enables the declaration of completely separate layouts
67
for various form factors. Especially with more complex layouts this might be
68
useful to improve and maintain a reasonable source code structure.
69
70
Note, that the \l{LayoutItemProxy} API is in a technology preview state and might
71
be subject to change or removal in future Qt versions.
72
73
\section1 Adaptive hierarchy, adaptive layout
74
75
More complex reconstructions of the layout might require changes to
76
the hierarchy. A small stand-alone button in a small layout might be
77
combined with other buttons and put into a box of a larger layout. An item
78
that is fully visible in one layout, might require a \l {Flickable} in
79
another, smaller layout. In this scenario, it is best to rely on the
80
\l {LayoutItemProxy}, which allows moving \l{Item}{Items} across various
81
hierarchy levels and between different \l{Item.parent}{parent} items.
82
83
The \l {Qt Quick Layouts - Responsive Layout Example} shows a case where an
84
item is moved between different hierarchy levels, put into a \l {Flickable}
85
in one case and on the top level in another layout. The two resulting
86
layouts look as follows.
87
88
\div {class="float-right"}
89
\image qtquicklayouts-example-responsivelayouts.png
90
{Two windows showing numbered grid tiles in different arrangements}
91
\enddiv
92
93
94
\section1 Useful links: Consult your design guidelines
95
96
Many design guidelines offer help and tips to create responsive layouts.
97
Implementing the respective techniques is possible with the APIs mentioned
98
above. For further information we recommend the following links:
99
100
\list
101
\li \l {https://developer.apple.com/design/human-interface-guidelines/layout}{Apple human interface guidelines}
102
\li \l {https://m3.material.io/foundations/layout/applying-layout/window-size-classes}{Material3 layouts}
103
\li \l {https://learn.microsoft.com/en-us/windows/apps/design/layout/responsive-design}{Microsoft Fluent responsive design techniques}
104
\endlist
105
106
*/
qtdeclarative
src
quick
doc
src
concepts
layouts
qtquicklayouts-responsive.qdoc
Generated on
for Qt by
1.18.0