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. How to change Switch style in QML
Forum Updated to NodeBB v4.3 + New Features

How to change Switch style in QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 4.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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on last edited by
    #1

    Hi!
    How to change Switch style in QML?
    It's not work:

    import QtQuick 2.12
    import QtQuick.Controls.Styles 1.4
    import QtQuick.Controls 2.12
    import AppCore 1.0
     
     
        Switch {
            id: element
            x: 248
            y: 39
            text: qsTr("Switch")
            focusPolicy: Qt.WheelFocus
            style: SwitchStyle {
                groove: Rectangle {
                        implicitWidth: 100
                        implicitHeight: 20
                        radius: 9
                        border.color: control.activeFocus ? "darkblue" : "gray"
                        border.width: 1
                }
            }
        }
    
    1 Reply Last reply
    0
    • Shrinidhi UpadhyayaS Offline
      Shrinidhi UpadhyayaS Offline
      Shrinidhi Upadhyaya
      wrote on last edited by
      #2

      Hi @Mikeeeeee , you can make use of the properties like background, indicator etc and customize or change the style of your switch.
      For more information you can go through the documentation[https://doc.qt.io/qt-5/qtquickcontrols2-customize.html#customizing-switch]

      Sample Code:-

       Switch {
              id: control
              checked: true
      
              indicator: Rectangle {
                  implicitWidth: 48
                  implicitHeight: 26
                  x: control.width - width - control.rightPadding
                  y: parent.height / 2 - height / 2
                  radius: 13
                  color: control.checked ? "green" : "red"
                  border.color: "black"
      
                  Rectangle {
                      x: control.checked ? parent.width - width : 0
                      width: 26
                      height: 26
                      radius: 13
                      border.color: "black"
                  }
              }
          }
      

      Sample Output:-

      0_1567994770210_31408df6-0cbd-43b5-948e-0ed969ff09fd-image.png

      Shrinidhi Upadhyaya.
      Upvote the answer(s) that helped you to solve the issue.

      1 Reply Last reply
      3
      • M Offline
        M Offline
        Mikeeeeee
        wrote on last edited by
        #3
        This post is deleted!
        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