Visual Emements in QML

Rectangle Type

  1. import QtQuick 2.3
  2. Item {
  3. width: 320
  4. height: 480
  5. Rectangle {
  6. color: "#272822"
  7. width: 320
  8. height: 480
  9. }
  10. // This element displays a rectangle with a gradient and a border
  11. Rectangle {
  12. x: 160
  13. y: 20
  14. width: 100
  15. height: 100
  16. radius: 8 // This gives rounded corners to the Rectangle
  17. gradient: Gradient { // This sets a vertical gradient fill
  18. GradientStop { position: 0.0; color: "aqua" }
  19. GradientStop { position: 1.0; color: "teal" }
  20. }
  21. border { width: 3; color: "white" } // This sets a 3px wide black border to be drawn
  22. }
  23. // This rectangle is a plain color with no border
  24. Rectangle {
  25. x: 40
  26. y: 20
  27. width: 100
  28. height: 100
  29. color: "red"
  30. }
  31. }

Image Type

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

Shared Visual Properties

Opacity and Visibility
  1. import QtQuick 2.3
  2. Item {
  3. width: 320
  4. height: 480
  5. Rectangle {
  6. color: "#272822"
  7. width: 320
  8. height: 480
  9. }
  10. Item {
  11. x: 20
  12. y: 270
  13. width: 200
  14. height: 200
  15. MouseArea {
  16. anchors.fill: parent
  17. onClicked: topRect.visible = !topRect.visible
  18. }
  19. Rectangle {
  20. x: 20
  21. y: 20
  22. width: 100
  23. height: 100
  24. color: "red"
  25. }
  26. Rectangle {
  27. id: topRect
  28. opacity: 0.5
  29. x: 100
  30. y: 100
  31. width: 100
  32. height: 100
  33. color: "blue"
  34. }
  35. }
  36. }

Transforms
  1. import QtQuick 2.3
  2. Item {
  3. width: 320
  4. height: 480
  5. Rectangle {
  6. color: "#272822"
  7. width: 320
  8. height: 480
  9. }
  10. Rectangle {
  11. rotation: 45 // This rotates the Rectangle by 45 degrees
  12. x: 20
  13. y: 160
  14. width: 100
  15. height: 100
  16. color: "blue"
  17. }
  18. Rectangle {
  19. scale: 0.8 // This scales the Rectangle down to 80% size
  20. x: 160
  21. y: 160
  22. width: 100
  23. height: 100
  24. color: "green"
  25. }
  26. }

Supported types of user input

Mouse and touch events

  1. import QtQuick 2.12
  2. Item {
  3. id: root
  4. width: 320
  5. height: 480
  6. Rectangle {
  7. color: "#272822"
  8. width: 320
  9. height: 480
  10. }
  11. Rectangle {
  12. id: rectangle
  13. x: 40
  14. y: 20
  15. width: 120
  16. height: 120
  17. color: "red"
  18. TapHandler {
  19. onTapped: rectangle.width += 10
  20. }
  21. }
  22. }

Keyboard and button events

  1. import QtQuick 2.3
  2. Item {
  3. id: root
  4. width: 320
  5. height: 480
  6. Rectangle {
  7. color: "#272822"
  8. width: 320
  9. height: 480
  10. }
  11. Rectangle {
  12. id: rectangle
  13. x: 40
  14. y: 20
  15. width: 120
  16. height: 120
  17. color: "red"
  18. focus: true
  19. Keys.onUpPressed: rectangle.y -= 10
  20. Keys.onDownPressed: rectangle.y += 10
  21. Keys.onLeftPressed: rectangle.x += 10
  22. Keys.onRightPressed: rectangle.x -= 10
  23. }
  24. }
  1. import QtQuick 2.12
  2. import QtQuick.Controls 2.4
  3. import QtQuick.Layouts 1.3
  4. ApplicationWindow {
  5. width: 300
  6. height: 200
  7. visible: true
  8. ColumnLayout {
  9. anchors.fill: parent
  10. TextField {
  11. id: singleline
  12. text: "Initial Text"
  13. Layout.alignment: Qt.AlignHCenter | Qt.AlignTop
  14. Layout.margins: 5
  15. background: Rectangle {
  16. implicitWidth: 200
  17. implicitHeight: 40
  18. border.color: singleline.focus ? "#21be2b" : "lightgray"
  19. color: singleline.focus ? "lightgray" : "transparent"
  20. }
  21. }
  22. TextArea {
  23. id: multiline
  24. placeholderText: "Initial text\n...\n...\n"
  25. Layout.alignment: Qt.AlignLeft
  26. Layout.fillWidth: true
  27. Layout.fillHeight: true
  28. Layout.margins: 5
  29. background: Rectangle {
  30. implicitWidth: 200
  31. implicitHeight: 100
  32. border.color: multiline.focus ? "#21be2b" : "lightgray"
  33. color: multiline.focus ? "lightgray" : "transparent"
  34. }
  35. }
  36. }
  37. }