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. Switching visibility of a Rectangle via button

Switching visibility of a Rectangle via button

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 1.2k Views
  • 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.
  • A Offline
    A Offline
    ACaldas
    wrote on last edited by
    #1

    Hi all.
    I have a rectangle that I need to switch between visible / not visible via "myButton". So the button should change alternately the rectangle visibility. (with two states I think)...
    I've tried to make the button "checkable" in order to apply the condition below but doesn't work at all. What am I doing wrong?

    my main.qml:

    Button {
    	id: myButton
    	width: 80	
    	height: 20
            text: "click me"
    	checkable: true
    	onClicked: if (checked){
    		myRectangle.visible = true
    	}else{
    		myRectangle.visible = false
    	     }
    	}
    

    P.S.: Transparency is not ok for this case. So Opacity is not applicable as myRectangle content will keep active and I need to avoid any user interaction when the rectangle content is "invisible". that's why I need to make it not visible.

    Can you please help me? Thanks.

    MarkkyboyM eyllanescE 2 Replies Last reply
    0
    • A ACaldas

      Hi all.
      I have a rectangle that I need to switch between visible / not visible via "myButton". So the button should change alternately the rectangle visibility. (with two states I think)...
      I've tried to make the button "checkable" in order to apply the condition below but doesn't work at all. What am I doing wrong?

      my main.qml:

      Button {
      	id: myButton
      	width: 80	
      	height: 20
              text: "click me"
      	checkable: true
      	onClicked: if (checked){
      		myRectangle.visible = true
      	}else{
      		myRectangle.visible = false
      	     }
      	}
      

      P.S.: Transparency is not ok for this case. So Opacity is not applicable as myRectangle content will keep active and I need to avoid any user interaction when the rectangle content is "invisible". that's why I need to make it not visible.

      Can you please help me? Thanks.

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

      Try this;

      main.qml

      import QtQuick 2.12
      import QtQuick.Window 2.12
      import QtQuick.Controls 2.12
      
      Window {
          width: 200
          height: 200
          visible: true
          title: qsTr("Hello World")
      
          Rectangle {
              id: myRectangle
              width: 100
              height: width
              color: "red"
              anchors.centerIn: parent
          }
          Button {
              id: myButton
              width: 80
              height: 20
              text: "click me"
              checked: true
              onClicked: {
                  if   (myRectangle.visible === false)
                        myRectangle.visible =  !false
                  else  myRectangle.visible =   false
              }
              anchors {
                  bottom: myRectangle.top
                  bottomMargin: 10
                  horizontalCenter: parent.horizontalCenter
              }
          }
      }
      

      Also, this may help too; https://stackoverflow.com/questions/46059068/setting-checked-property-of-a-button-in-qml

      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
      1
      • A ACaldas

        Hi all.
        I have a rectangle that I need to switch between visible / not visible via "myButton". So the button should change alternately the rectangle visibility. (with two states I think)...
        I've tried to make the button "checkable" in order to apply the condition below but doesn't work at all. What am I doing wrong?

        my main.qml:

        Button {
        	id: myButton
        	width: 80	
        	height: 20
                text: "click me"
        	checkable: true
        	onClicked: if (checked){
        		myRectangle.visible = true
        	}else{
        		myRectangle.visible = false
        	     }
        	}
        

        P.S.: Transparency is not ok for this case. So Opacity is not applicable as myRectangle content will keep active and I need to avoid any user interaction when the rectangle content is "invisible". that's why I need to make it not visible.

        Can you please help me? Thanks.

        eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on last edited by eyllanesc
        #3

        @ACaldas

            Rectangle {
                id: myRectangle
                visible: myButton.checked
                // other values
            }
            Button {
                id: myButton
                width: 80
                height: 20
                text: "click me"
                checkable: true
            }
        

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        1 Reply Last reply
        1
        • A Offline
          A Offline
          ACaldas
          wrote on last edited by ACaldas
          #4

          Thanks gents,

          @eyllanesc is it possible to bind the myRectangle visibility to 2 buttons? Something like this:

          Rectangle {
             id: myRectangle
             visible:  myButton.checked : myButton2.checked
          

          no idea of the correct syntax...

          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