Qt Quick Diseños: ejemplo de diseño adaptativo
Muestra cómo utilizar « LayoutItemProxy » para crear una interfaz de usuario adaptativa.
Este ejemplo muestra cómo utilizar LayoutProxyItems en combinación con diseños para crear diseños adaptativos.
Ejecución del ejemplo
Puedes ejecutar el ejemplo desde:
- Qt Creator
Abre el modo « Welcome » y selecciona el ejemplo en Examples. Para obtener más información, consulta Qt Creator: Tutorial: Compilar y ejecutar.
- Qt Extension for Visual Studio Code
Ejecute el comando « Qt: Open Qt examples » desde Command Palette y seleccione el ejemplo de la lista. Para obtener más información, consulte Qt Extension for Visual Studio Code: Tutorial: Compilar y ejecutar.
Creación de elementos
El tipo ` LayoutItemProxy ` permite utilizar el mismo elemento en diferentes diseños, aunque solo puede estar visible un diseño a la vez. Esto se puede utilizar para crear diseños adaptativos que se ajusten al tamaño de la ventana o de la pantalla.

En primer lugar, debemos definir todos los elementos que deben aparecer en nuestra interfaz de usuario en algún momento. Utilizamos un `AnnotatedRect`, que es un simple `Rectangle` con texto añadido.
Rectangle {
id: contentItem
Layout.fillWidth: true
implicitHeight: grid.implicitHeight
implicitWidth: grid.implicitWidth
color: "#00414A"
GridLayout {
id: grid
anchors {
fill: parent
margins: 8
}
columns: Math.max(1, Math.min(Math.round(width / 130), 6))
Repeater {
model: 60
delegate: Rectangle {
required property int index
Layout.fillWidth: true
Layout.margins: 8
implicitWidth: 200
implicitHeight: width
radius: width / 10
gradient: Gradient {
GradientStop { position: -0.2; color: "#2CDE85" }
GradientStop { position: 1.2; color: "#00414A" }
}
Text {
color: "#ffffff"
font.pointSize: 22
anchors.centerIn: parent
text: parent.index + 1
}
}
}
}
}
Button {
id: a
text: "Text"
icon.source: "./icons/text.svg"
Layout.fillWidth: true
Layout.margins: 3
}
Button {
id: b
text: "Grid 1"
icon.source: "./icons/grid.svg"
Layout.fillWidth: true
Layout.margins: 3
}
Button {
id: c
text: "Grid 2"
icon.source: "./icons/grid.svg"
Layout.fillWidth: true
Layout.margins: 3
}
Button {
id: d
text: "Settings"
icon.source: "./icons/settings.svg"
Layout.fillWidth: true
Layout.margins: 3
}Creación de diseños
Ahora podemos declarar varios diseños utilizando ` LayoutItemProxies`, dirigiéndolos a los elementos declarados anteriormente. Un diseño individual se puede definir de la siguiente manera.
ColumnLayout {
id: smallLayout
anchors.fill: parent
Flickable {
Layout.fillHeight: true
Layout.fillWidth: true
contentWidth: width
contentHeight: gl.implicitHeight
clip: true
ScrollIndicator.vertical: ScrollIndicator { }
LayoutItemProxy {
id: gl
width: parent.width
height: implicitHeight
target: contentItem
}
}
RowLayout {
Layout.fillHeight: false
Layout.fillWidth: true
Layout.margins: 5
LayoutItemProxy{ target: a; }
LayoutItemProxy{ target: b; }
LayoutItemProxy{ target: c; }
}
}Este fragmento de código muestra varias formas de utilizar ` LayoutItemProxy`. El método más sencillo consiste en añadir ` LayoutItemProxies ` a un diseño, como el ` RowLayout ` que se muestra aquí. Además, establecemos una propiedad adjunta adicional « Layout » en el «LayoutProxyItem» que afectará al elemento de destino únicamente en este diseño concreto. Por otra parte, vemos que el elemento « d » no se utiliza en el primer diseño. A continuación, queda oculto automáticamente por el « LayoutItemProxy » en el segundo diseño. Otra forma de utilizarlo consiste en establecer un « LayoutItemProxy » como contenido de un « Flickable ».
Se declara otro diseño de la siguiente manera.
RowLayout {
id: largeLayout
anchors.fill: parent
ColumnLayout {
Layout.minimumWidth: 100
Layout.fillWidth: true
Layout.margins: 2
LayoutItemProxy{ target: a }
LayoutItemProxy{ target: b }
LayoutItemProxy{ target: c }
Item { Layout.fillHeight: true }
LayoutItemProxy{ target: d }
}
LayoutItemProxy {
Layout.fillHeight: true
Layout.fillWidth: true
target: contentItem
}
}Aquí mostramos que LayoutItemProxies se puede utilizar junto con Items reales en el mismo nivel jerárquico. En general, LayoutItemProxy es flexible y permite estructuras anidadas de elementos y diseños.
Configuración de la disposición
Una vez definidos los dos diseños, smallLayout y largeLayout, podemos continuar configurando el diseño que se adapte al tamaño actual de la aplicación. Definimos una nueva función para este código que llamaremos cuando se inicialice la ventana y cada vez que cambie el ancho:
function setFittingLayout() {
if (width < 450) {
smallLayout.visible = true
largeLayout.visible = false
} else {
smallLayout.visible = false
largeLayout.visible = true
}
}
onWidthChanged: setFittingLayout()
Component.onCompleted: setFittingLayout()Como alternativa a llamar a esta función tras la inicialización, podemos ocultar todos los diseños excepto el adecuado para el tamaño inicial en el código declarativo.
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.