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. [SOLVED] Find all mouse areas and assign click event
QtWS25 Last Chance

[SOLVED] Find all mouse areas and assign click event

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 584 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.
  • P Offline
    P Offline
    Per-Erik Kristensson
    wrote on last edited by
    #1

    I have a row with several mouse areas, see the QML-code below. When the page has been loaded I want to assign what will happen on each click via javascript. I have two questions:

    Now I put all mouse areas in an array with the id of the mouse area. Could I do something more clever and find all mouse areas in the row (via childrenAt or similar)?

    Now when I click on an area it seems that always the last mouse area has been click (the last value of outputString is always used). How do I solve this?

    @
    import QtQuick 1.1

    Rectangle {
    width: 360
    height: 360

    Row {
        id: masterRow
        x: 0
        y: 0
    
        Rectangle {
            width: 100
            height: 100
            color: "red"
    
            MouseArea {
                id: mouseAreaRed
                anchors.fill: parent
            }
        }
    
        Rectangle {
            width: 100
            height: 100
            color: "yellow"
    
            MouseArea {
                id: mouseAreaYellow
                anchors.fill: parent
            }
        }
    
        Rectangle {
            width: 100
            height: 100
            color: "green"
    
            MouseArea {
                id: mouseAreaGreen
                anchors.fill: parent
            }
        }
    }
    
    Component.onCompleted:
    {
        var areas = [];
        //I don't like this.
        areas.push(mouseAreaRed);
        areas.push(mouseAreaYellow);
        areas.push(mouseAreaGreen);
    
        for(var i=0; i<areas.length; i++)
        {
            var area = areas[i];
            var outputString = "Mouse area " + i + " was clicked.";
            area.clicked.connect( function()
            {
                //Bug: outputString is only correct for the last item.
                console.log(outputString);
            });
        }
    }
    

    }

    @

    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