Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Colored rectangle
Forum Updated to NodeBB v4.3 + New Features

Colored rectangle

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GrahamLa
    wrote on last edited by
    #1

    Hi
    Is it possible to color parts of a rectangle with different colors?

    J 1 Reply Last reply
    0
    • G GrahamLa

      Hi
      Is it possible to color parts of a rectangle with different colors?

      J Offline
      J Offline
      JasmineSethi
      wrote on last edited by
      #2

      @GrahamLa yes it is possible. But can you explain what exactly you want?

      G 1 Reply Last reply
      0
      • J JasmineSethi

        @GrahamLa yes it is possible. But can you explain what exactly you want?

        G Offline
        G Offline
        GrahamLa
        wrote on last edited by
        #3

        @JasmineSethi
        Hi
        I need a control that looks like the image below.
        Each area is clicklable and change color

        0_1549009246120_69090d15-6b75-4e53-90bf-e60d072f9480-image.png

        J ODБOïO 2 Replies Last reply
        0
        • G GrahamLa

          @JasmineSethi
          Hi
          I need a control that looks like the image below.
          Each area is clicklable and change color

          0_1549009246120_69090d15-6b75-4e53-90bf-e60d072f9480-image.png

          J Offline
          J Offline
          JasmineSethi
          wrote on last edited by
          #4

          @GrahamLa check if this can slove ypur problem. Also adjust width,height, radius as u want.

          import QtQuick 2.9
          import QtQuick.Window 2.2

          Window
          {
          id: appWindow
          visible: true
          width: 500
          height: 600

          Rectangle
          {
              width: parent.width/2
              height: parent.height/10
              color: "black"
              anchors.centerIn: parent
              radius: 10
              Rectangle
              {
                  id: r1
                  width: parent.width/3
                  height: parent.height
                  color: "transparent"
                  radius: 5
                  Text {
                      id: name
                      text: qsTr("CEP")
                      color: "white"
                      font.pixelSize: 20
                      anchors.centerIn: parent
                  }
                  MouseArea
                  {
                      anchors.fill: parent
                      onClicked:
                      {
                          r1.color = "yellow"
                          r2.color = "transparent"
                          r3.color = "transparent"
                      }
                  }
              }
              Rectangle
              {
                  id: r2
                  width: parent.width/3
                  height: parent.height
                  color: "transparent"
                  radius: 5
                  anchors.left: r1.right
                  Text {
                      //id: name
                      text: qsTr("DEP")
                      color: "white"
                      font.pixelSize: 20
                      anchors.centerIn: parent
                  }
                  MouseArea
                  {
                      anchors.fill: parent
                      onClicked:
                      {
                          r2.color = "yellow"
                          r1.color = "transparent"
                          r3.color = "transparent"
                      }
                  }
              }
              Rectangle
              {
                  id: r3
                  width: parent.width/3
                  height: parent.height
                  color: "transparent"
                  radius: 5
                  anchors.left: r2.right
                  Text {
                      //id: name
                      text: qsTr("PEP")
                      color: "white"
                      font.pixelSize: 20
                      anchors.centerIn: parent
                  }
                  MouseArea
                  {
                      anchors.fill: parent
                      onClicked:
                      {
                          r3.color = "yellow"
                          r2.color = "transparent"
                          r1.color = "transparent"
                      }
                  }
              }
          }
          

          }

          G 1 Reply Last reply
          1
          • G GrahamLa

            @JasmineSethi
            Hi
            I need a control that looks like the image below.
            Each area is clicklable and change color

            0_1549009246120_69090d15-6b75-4e53-90bf-e60d072f9480-image.png

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by
            #5

            @GrahamLa
            You can use clip property for that

               Item {          
                        clip: true
                        Rectangle {
                            anchors.fill: parent
                            anchors.rightMargin: -radius
                            radius: 10
                        }
                    }
            

            example :

            import QtQuick 2.9
            import QtQuick.Window 2.2
            import QtQuick.Layouts 1.3
            
            Window
            {
                id: appWindow
                visible: true
                width: 500
                height: 600
            
                property int currentSelected : 0
                property string defaultColor: "grey"
            
                RowLayout{
                    width: parent.width*0.8
                    anchors.horizontalCenter: parent.horizontalCenter
                    height: 90
            
                    Item {
                        Layout.fillWidth: true
                        Layout.fillHeight: true
                        clip: true
                        Rectangle {
                            anchors.fill: parent
                            anchors.rightMargin: -radius
                            radius: 10
                            color: currentSelected == 0 ? "red" : defaultColor
                            MouseArea{
                                anchors.fill: parent
                                onClicked: currentSelected=0
                            }
                        }
                    }
                    Rectangle{
                        Layout.fillWidth: true
                        Layout.fillHeight: true
                        color: currentSelected == 1 ? "green" : defaultColor
                        MouseArea{
                            anchors.fill: parent
                            onClicked: currentSelected=1
                        }
                    }
                    Item {
                        clip: true
                        Layout.fillWidth: true
                        Layout.fillHeight: true
                        Rectangle {
                            anchors.fill: parent
                            anchors.leftMargin: -radius
                            radius: 10
                            color: currentSelected == 2 ? "blue" : defaultColor
                            opacity: 0.5
                            MouseArea{
                                anchors.fill: parent
                                onClicked: currentSelected=2
                            }
                        }
                    }
                }
            }
            
            
            1 Reply Last reply
            0
            • J JasmineSethi

              @GrahamLa check if this can slove ypur problem. Also adjust width,height, radius as u want.

              import QtQuick 2.9
              import QtQuick.Window 2.2

              Window
              {
              id: appWindow
              visible: true
              width: 500
              height: 600

              Rectangle
              {
                  width: parent.width/2
                  height: parent.height/10
                  color: "black"
                  anchors.centerIn: parent
                  radius: 10
                  Rectangle
                  {
                      id: r1
                      width: parent.width/3
                      height: parent.height
                      color: "transparent"
                      radius: 5
                      Text {
                          id: name
                          text: qsTr("CEP")
                          color: "white"
                          font.pixelSize: 20
                          anchors.centerIn: parent
                      }
                      MouseArea
                      {
                          anchors.fill: parent
                          onClicked:
                          {
                              r1.color = "yellow"
                              r2.color = "transparent"
                              r3.color = "transparent"
                          }
                      }
                  }
                  Rectangle
                  {
                      id: r2
                      width: parent.width/3
                      height: parent.height
                      color: "transparent"
                      radius: 5
                      anchors.left: r1.right
                      Text {
                          //id: name
                          text: qsTr("DEP")
                          color: "white"
                          font.pixelSize: 20
                          anchors.centerIn: parent
                      }
                      MouseArea
                      {
                          anchors.fill: parent
                          onClicked:
                          {
                              r2.color = "yellow"
                              r1.color = "transparent"
                              r3.color = "transparent"
                          }
                      }
                  }
                  Rectangle
                  {
                      id: r3
                      width: parent.width/3
                      height: parent.height
                      color: "transparent"
                      radius: 5
                      anchors.left: r2.right
                      Text {
                          //id: name
                          text: qsTr("PEP")
                          color: "white"
                          font.pixelSize: 20
                          anchors.centerIn: parent
                      }
                      MouseArea
                      {
                          anchors.fill: parent
                          onClicked:
                          {
                              r3.color = "yellow"
                              r2.color = "transparent"
                              r1.color = "transparent"
                          }
                      }
                  }
              }
              

              }

              G Offline
              G Offline
              GrahamLa
              wrote on last edited by
              #6

              @JasmineSethi
              Thank you so much it worked

              J 1 Reply Last reply
              0
              • G GrahamLa

                @JasmineSethi
                Thank you so much it worked

                J Offline
                J Offline
                JasmineSethi
                wrote on last edited by
                #7

                @GrahamLa mark your question as solved :)

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved