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. ChildAt() in QML , a question .i need help,thank you!
QtWS25 Last Chance

ChildAt() in QML , a question .i need help,thank you!

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 3 Posters 3.6k 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.
  • H Offline
    H Offline
    hill
    wrote on last edited by
    #1

    i am a new learner in QML, i want to make a little game.
    so ,in mousearer,i want to click a Lable ,and make it to move ,i use the childAt(mouseX,mouse Y) to capture it.

    but ,i do not know how to use childAt,i need help.
    thank you!

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Please, use correct interpunction, your post is very hard to read.

      If you don't know how to use this at all, read the docs. If you have any specific problem, please specify what is wrong.

      (Z(:^

      1 Reply Last reply
      0
      • p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #3

        Perhaps this is what you want,

        @
        import QtQuick 2.0
        import QtQuick.Controls 1.0

        Rectangle {
        width: 800
        height: 480
        Text {
        text: qsTr("Drag the two labels")
        anchors.centerIn: parent
        }

        Label {
            id: l1
            text: "Label 1"
            x: 50
            y: 50
            width: 100
            height: 100
            color: "red"
        
            MouseArea {
                anchors.fill: parent
                property variant previousPosition
                onPressed: {
                    previousPosition = Qt.point(mouseX, mouseY)
                }
                onPositionChanged: {
                    if (pressedButtons == Qt.LeftButton) {
                        var dx = mouseX - previousPosition.x
                        var dy = mouseY - previousPosition.y
                        l1.x = l1.x + dx;
                        l1.y = l1.y + dy;
                    }
                }
            }
        }
        
        Label {
            id: l2
            text: "Label 2"
            x: 100
            y: 100
            width: 100
            height: 100
            color: "orange"
        
            MouseArea {
                anchors.fill: parent
                property variant previousPosition
                onPressed: {
                    previousPosition = Qt.point(mouseX, mouseY)
                }
                onPositionChanged: {
                    if (pressedButtons == Qt.LeftButton) {
                        var dx = mouseX - previousPosition.x
                        var dy = mouseY - previousPosition.y
                        l2.x = l2.x + dx;
                        l2.y = l2.y + dy;
                    }
                }
            }
        }
        

        }

        @

        157

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hill
          wrote on last edited by
          #4

          Thank you ,p3c0!
          But,i want to automatically capture the Lable ,which in the Rectangle.
          eg: l1 is captured by (mouseX,mouseY).
          so i think childAt() can be used. But i cannot use it.

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            Sorry but i didn't get you. In your first post you said you want to click the label and and move it.
            How would you get the X an Y coordinates of the item unless you click it or you know in advance the position of each label ?
            Can you elaborate more your problem ?

            157

            1 Reply Last reply
            0
            • H Offline
              H Offline
              hill
              wrote on last edited by
              #6

              hi,p3c0.
              thank you for your help.
              in my program. i defined 7 lables.

              Mousearea{
              anchors.fill:parent
              onClicked:{
              var click;
              click = mapToItem(parent,mouseX,mouseY);
              parent.childAt(click.x,click.y).anchors.leftMargin = 50;
              ...
              }
              }

              like these,i want capture a lable by (mouseX,mouseY),but ,in running,
              my program return wrong value.

              1 Reply Last reply
              0
              • p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                Hi,

                Considering your requirement i implemented the following and it gives the item (its objectName )when clicked on it,

                @
                import QtQuick 2.0
                import QtQuick.Controls 1.0

                Rectangle {
                id: rect
                width: 800
                height: 480
                Text {
                text: qsTr("Click on each item")
                anchors.centerIn: parent
                }

                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        var click;
                        click = mapToItem(parent,mouseX,mouseY)
                        var obj = parent.childAt(mouseX,mouseY)
                        console.log(obj.objectName)
                    }
                }
                
                Label {
                    id: l1
                    objectName: "l1"
                    text: "Label 1"
                    x: 50
                    y: 50
                    width: 100
                    height: 100
                    color: "red"
                }
                
                Label {
                    id: l2
                    objectName: "l2"
                    text: "Label 2"
                    x: 100
                    y: 100
                    width: 100
                    height: 100
                    color: "orange"
                }
                
                Rectangle {
                    objectName: "r2"
                    width: 200
                    height: 200
                    x: 500
                    y: 100
                    color: "red"
                }
                

                }
                @

                The MouseArea code is similar to your's but i moved it to the top before adding the two labels and the rect. If i move the MouseArea code after creation of labels and the rect it gives QQuickMouseArea as the child.
                Hope this is what you want.

                157

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  hill
                  wrote on last edited by
                  #8

                  Thank you, p3c0.
                  your answer is my need ,thank you so much!

                  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