Qt Reference Documentation

QML Rectangle Element

The Rectangle item allows you to add rectangles to a scene. More...

Inherits Item

  • List of all members, including inherited members
  • Properties

    Detailed Description

    A Rectangle is painted using a solid fill (color) and an optional border. You can also create rounded rectangles using the radius property.

     import Qt 4.7
    
     Rectangle {
         width: 100
         height: 100
         color: "red"
         border.color: "black"
         border.width: 5
         radius: 10
     }

    Property Documentation

    border.width : int

    border.color : color

    The width and color used to draw the border of the rectangle.

    A width of 1 creates a thin line. For no line, use a width of 0 or a transparent color.

    If border.width is an odd number, the rectangle is painted at a half-pixel offset to retain border smoothness. Also, the border is rendered evenly on either side of the rectangle's boundaries, and the spare pixel is rendered to the right and below the rectangle (as documented for QRect rendering). This can cause unintended effects if border.width is 1 and the rectangle is clipped by a parent item:

     Rectangle {
         width: 100; height: 100
         color: "yellow"
    
         Rectangle {
             anchors.fill: parent
             anchors.margins: 10
             clip: true
    
             Rectangle {
                 anchors.fill: parent
                 border.width: 1
             }
         }
     }

    Here, the innermost rectangle's border is clipped on the bottom and right edges by its parent. To avoid this, the border width can be set to two instead of one.


    color : color

    This property holds the color used to fill the rectangle.

     // green rectangle using hexidecimal notation
     Rectangle { color: "#00FF00" }
    
     // steelblue rectangle using SVG color name
     Rectangle { color: "steelblue" }

    The default color is white.

    If both a gradient and a color are specified, the gradient will be used.


    gradient : Gradient

    The gradient to use to fill the rectangle.

    This property allows for the construction of simple vertical gradients. Other gradients may by formed by adding rotation to the rectangle.

     Rectangle {
         y: 0; width: 80; height: 80
         color: "lightsteelblue"
     }
    
     Rectangle {
         y: 100; width: 80; height: 80
         gradient: Gradient {
             GradientStop { position: 0.0; color: "lightsteelblue" }
             GradientStop { position: 1.0; color: "blue" }
         }
     }
    
     Rectangle {
         y: 200; width: 80; height: 80
         rotation: 90
         gradient: Gradient {
             GradientStop { position: 0.0; color: "lightsteelblue" }
             GradientStop { position: 1.0; color: "blue" }
         }
     }

    If both a gradient and a color are specified, the gradient will be used.

    See also Gradient and color.


    radius : real

    This property holds the corner radius used to draw a rounded rectangle.

    If radius is non-zero, the rectangle will be painted as a rounded rectangle, otherwise it will be painted as a normal rectangle. The same radius is used by all 4 corners; there is currently no way to specify different radii for different corners.


    smooth : bool

    Set this property if you want the item to be smoothly scaled or transformed. Smooth filtering gives better visual quality, but is slower. If the item is displayed at its natural size, this property has no visual or performance effect.

    Note: Generally scaling artifacts are only visible if the item is stationary on the screen. A common pattern when animating an item is to disable smooth filtering at the beginning of the animation and reenable it at the conclusion.

    On this image, smooth is turned off on the top half and on on the bottom half.


    X

    Thank you for giving your feedback.

    Make sure it is related to this specific page. For more general bugs and requests, please use the Qt Bug Tracker.