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. Swtich button - disable switch with spacebar
Forum Updated to NodeBB v4.3 + New Features

Swtich button - disable switch with spacebar

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 416 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.
  • W Offline
    W Offline
    Witc
    wrote on last edited by
    #1

    Hi,

    I am using Switch "button" and when I press the spacebar key the switch is toggling. How can I disable it? I want to toggle it only when I click by mouse on it. Thank you!

    Switch {
                        id: control
                        text: qsTr("mode")
                        font.pixelSize: 16*Constants.PYSIDE6_TO_2_RESIZE
                        font.bold: false
                        font.italic: true
                        checked:true
    
                         anchors.left: btnStart.horizontalCenter
                         anchors.top: btnStart.top
                         //anchors.topMargin: 60
                         anchors.leftMargin: 130
                        
                        indicator: Rectangle {
                            implicitWidth: 45
                            implicitHeight: 16
                            anchors.top: control.top
                            anchors.topMargin: 5
                           // x: control.width - width - control.rightPadding
                            //y: control.top  //- height / 2
                            radius: 10
                            color: control.checked ? Constants.XGREEN : Constants.XWHITE
                            border.color: "black"
    
                            Rectangle {
                                x: control.checked ? parent.width - width : 0
                                width: 22
                                height: 22
                                radius: 11
                                anchors.top: parent.top
                                anchors.topMargin: -3
                                border.color: "black"
                            }
                        }
    
                        contentItem: Text {
                            text: control.text
                            font: control.font
                            opacity: enabled ? 1.0 : 0.3
                            color: Constants.XWHITE 
                            verticalAlignment: Text.AlignVCenter
                            leftPadding: control.indicator.width + control.spacing
                        }
    }
    
    1 Reply Last reply
    0
    • W Offline
      W Offline
      Witc
      wrote on last edited by
      #4

      solved by adding: focusPolicy: Qt.NoFocus to Switch

      Switch {
              id: switchBtn
              text: qsTr("mode")
              font.pixelSize: 16
              font.bold: false
              font.italic: true
              checked: false
              focusPolicy: Qt.NoFocus
          }
      
      1 Reply Last reply
      1
      • MarkkyboyM Offline
        MarkkyboyM Offline
        Markkyboy
        wrote on last edited by
        #2

        Add this to the bottom of Switch, seems to do the job;

        Keys.onSpacePressed: control.checked = false
        

        Don't just sit there standing around, pick up a shovel and sweep up!

        I live by the sea, not in it.

        1 Reply Last reply
        0
        • W Offline
          W Offline
          Witc
          wrote on last edited by Witc
          #3

          Hi, Thank you!
          I helps me by 50% :-) . Your solution can not the switch "turns on" , but it still "turns it off".
          :-(

          MarkkyboyM 1 Reply Last reply
          0
          • W Offline
            W Offline
            Witc
            wrote on last edited by
            #4

            solved by adding: focusPolicy: Qt.NoFocus to Switch

            Switch {
                    id: switchBtn
                    text: qsTr("mode")
                    font.pixelSize: 16
                    font.bold: false
                    font.italic: true
                    checked: false
                    focusPolicy: Qt.NoFocus
                }
            
            1 Reply Last reply
            1
            • W Witc

              Hi, Thank you!
              I helps me by 50% :-) . Your solution can not the switch "turns on" , but it still "turns it off".
              :-(

              MarkkyboyM Offline
              MarkkyboyM Offline
              Markkyboy
              wrote on last edited by
              #5

              @Witc - you're 100% correct. Yet when i first added that line it appeared to work and yet i can see by the code it wasn't going to work.......brain fart!!, my bad. Apologies for a crap reply! :D

              Don't just sit there standing around, pick up a shovel and sweep up!

              I live by the sea, not in it.

              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