En esta página

Tipos visuales

El tipo Rectángulo

Para los elementos visuales más básicos, Qt Quick se proporciona el tipo « Rectangle » para dibujar rectángulos. Estos rectángulos pueden colorearse con un color o un degradado vertical. El tipo « Rectangle » también permite dibujar bordes en el rectángulo.

Para dibujar formas personalizadas que no sean rectángulos, consulta el tipo « Canvas » o muestra una imagen prerenderizada utilizando el tipo « Image ».

import QtQuick

Item {

    width: 320
    height: 480

    Rectangle {
        color: "#272822"
        width: 320
        height: 480
    }

    // This element displays a rectangle with a gradient and a border
    Rectangle {
        x: 160
        y: 20
        width: 100
        height: 100
        radius: 8 // This gives rounded corners to the Rectangle
        gradient: Gradient { // This sets a vertical gradient fill
            GradientStop { position: 0.0; color: "aqua" }
            GradientStop { position: 1.0; color: "teal" }
        }
        border { width: 3; color: "white" } // This sets a 3px wide black border to be drawn
    }

    // This rectangle is a plain color with no border
    Rectangle {
        x: 40
        y: 20
        width: 100
        height: 100
        color: "red"
    }
}

Cuadrado y rectángulo redondeado con relleno degradado

El tipo «Image»

Qt Quick proporciona un tipo « Image » que puede utilizarse para mostrar imágenes. El tipo « Image » tiene una propiedad « source » cuyo valor puede ser una URL remota o local, o la URL de un archivo de imagen incrustado en un archivo de recursos compilado.

// This element displays an image. Because the source is online, it may take some time to fetch
Image {
    x: 40
    y: 20
    width: 61
    height: 73
    source: "http://codereview.qt-project.org/static/logo_qt.png"
}

Para imágenes más complejas, existen otros tipos similares a « Image ». « BorderImage » dibuja una imagen con escalado en cuadrícula, adecuado para imágenes utilizadas como bordes. « AnimatedImage » reproduce imágenes animadas en formato .gif y .mng. « AnimatedSprite » y « SpriteSequence » reproducen animaciones compuestas por múltiples fotogramas almacenados de forma adyacente en un formato de imagen no animado.

Para visualizar archivos de vídeo y datos de la cámara, consulta el Qt Multimedia módulo.

Propiedades visuales compartidas

Todos los elementos visuales proporcionados por Qt Quick se basan en el tipo «Elemento», que proporciona un conjunto común de atributos para los elementos visuales, incluidos los atributos de opacidad y transformación.

Opacidad y visibilidad

Los tipos de objetos QML proporcionados por Qt Quick incluyen soporte integrado para opacity. La opacidad se puede animar para permitir transiciones fluidas hacia o desde un estado transparente. La visibilidad también se puede gestionar de forma más eficiente con la propiedad visible, pero a costa de no poder animarla.

import QtQuick

Item {

    width: 320
    height: 480

    Rectangle {
        color: "#272822"
        width: 320
        height: 480
    }

    Item {
        x: 20
        y: 270
        width: 200
        height: 200
        MouseArea {
            anchors.fill: parent
            onClicked: topRect.visible = !topRect.visible
        }
        Rectangle {
            x: 20
            y: 20
            width: 100
            height: 100
            color: "red"
        }
        Rectangle {
            id: topRect
            opacity: 0.5

            x: 100
            y: 100
            width: 100
            height: 100
            color: "blue"
        }
    }
}

Cuadrados rojos y azules superpuestos que muestran la mezcla de opacidades

Las transformaciones

Qt Quick tienen soporte integrado para las transformaciones. Si deseas girar o escalar tu contenido visual, puedes establecer la propiedad Item::rotation o Item::scale. Estas propiedades también se pueden animar.

import QtQuick

Item {

    width: 320
    height: 480

    Rectangle {
        color: "#272822"
        width: 320
        height: 480
    }

    Rectangle {
        rotation: 45 // This rotates the Rectangle by 45 degrees
        x: 20
        y: 160
        width: 100
        height: 100
        color: "blue"
    }
    Rectangle {
        scale: 0.8 // This scales the Rectangle down to 80% size
        x: 160
        y: 160
        width: 100
        height: 100
        color: "green"
    }
}

Cuadrado azul girado 45 grados y cuadrado verde más pequeño

Para transformaciones más complejas, consulta la propiedad ` Item::transform `.

© 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.