Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How do I make a mouse click on one delegate and then highlight the other delegates with it?



  • Hi Guys

    Thanks for the great forum, it's helped a lot so far on my Qt journey. I couldn't find a solution to the following problem though.

    Item {
            Grid{
                spacing: 2
                columns: 10
                Repeater {
                    model: 100
                    delegate: Rectangle{
                        height: 20
                        width: 20
                        property bool colorBool: false
                        color: colorBool? "yellow" : "red"
                        MouseArea {
                            onClicked: colorBool = !colorBool
                        }
                    }
                }
            }
    

    I want to be able to click the mouse to change the color on one delegate, than without releasing the click move through the other delegates and have them change color too until I release the mouse click. Like drawing on very large pixels.
    Also to change the color back the same way, like an eraser.

    I've tried many things for many hours, I can't seem to figure anything out.

    Thanks!



  • https://qmlweb.github.io/

    import QtQuick 2.0
    
    
    Rectangle {
      id:root
      width: 500+5; height: width
      property bool press : false
      color: "green"
    	function draw(_it){
          if(_it.color=="green"){
          _it.color="red"
          }
          else{
          	_it.color="green"
          }
        }
    
      Grid{
      	height:500
        width:500
        columns:5
        spacing : 1
    	Repeater{
        	model:25
          	Rectangle{
              id:rec
            	height:100
                width:100
                color:"red"
              MouseArea{
                id:m
              	anchors.fill:parent
                hoverEnabled:true
                onClicked:{if(press)press=false;else press=true}
               
                onEntered:{
                  if(root.press==true)
                	draw(rec)
                }
              }
              
            }
        }    
      	
      }
    }
    

    if you want to hold mouse for drawing, you can put mouseX and mouseY as top level properties and check in your delegate if the coordinates are inside your delegate , that is some maths. i can help if you have probleme. but now i just reset my computer so i have to install Qt +creator



  • Thank you!

    That's pretty cool. I was sort of on the right track with something similar, but I don't think I would have figured out.

    I'm going to play with it a bit to get it just how I need it.


Log in to reply