Qt 6.11 is out! See what's new in the release
blog
mapToGlobal not working when use Layout.
-
In the code below, I tried to detect the change of the x coordinate of the blue rectangle when clicking on the green rectangle. Unfortunately, onGxChanged never executes. How can I deal with this problem?
import QtQuick 2.2 import QtQuick.Window 2.2 import QtQuick.Layouts 1.2 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") RowLayout { anchors.fill: parent Rectangle { id:rect color:"green" Layout.fillHeight: true Layout.fillWidth: true Layout.preferredWidth:25 MouseArea { anchors.fill: parent onPressed: rect.Layout.preferredWidth=50 } } Rectangle { color:"blue" Layout.fillHeight: true Layout.fillWidth: true Layout.preferredWidth:25 property int gx:parent.mapToGlobal((Qt.point(0,0))).x onGxChanged: console.debug("gx changed.") } } } -
Easy, that's because
gxdoesn't change at all. If you want to detect the change ofgx, you can do like thisproperty int gx: parent.mapToGlobal((Qt.point(x, y))).x@Kamichanw
ok that's working. but what about this code?import QtQuick 2.2 import QtQuick.Window 2.2 import QtQuick.Layouts 1.2 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") RowLayout { anchors.fill: parent Rectangle { id:rect color:"green" Layout.fillHeight: true Layout.fillWidth: true Layout.preferredWidth:25 MouseArea { anchors.fill: parent onPressed: rect.Layout.preferredWidth=50 } } Rectangle { color:"blue" Layout.fillHeight: true Layout.fillWidth: true Layout.preferredWidth:25 Rectangle { color:"yellow" width:20 height: 20 radius: 10 property int gx:parent.mapToGlobal((Qt.point(x,y))).x onGxChanged: console.debug("gx changed.") } } } } -
@Kamichanw
ok that's working. but what about this code?import QtQuick 2.2 import QtQuick.Window 2.2 import QtQuick.Layouts 1.2 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") RowLayout { anchors.fill: parent Rectangle { id:rect color:"green" Layout.fillHeight: true Layout.fillWidth: true Layout.preferredWidth:25 MouseArea { anchors.fill: parent onPressed: rect.Layout.preferredWidth=50 } } Rectangle { color:"blue" Layout.fillHeight: true Layout.fillWidth: true Layout.preferredWidth:25 Rectangle { color:"yellow" width:20 height: 20 radius: 10 property int gx:parent.mapToGlobal((Qt.point(x,y))).x onGxChanged: console.debug("gx changed.") } } } } -
Easy, that's because
gxdoesn't change at all. If you want to detect the change ofgx, you can do like thisproperty int gx: parent.mapToGlobal((Qt.point(x, y))).x -
@Kamichanw
ok that's working. but what about this code?import QtQuick 2.2 import QtQuick.Window 2.2 import QtQuick.Layouts 1.2 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") RowLayout { anchors.fill: parent Rectangle { id:rect color:"green" Layout.fillHeight: true Layout.fillWidth: true Layout.preferredWidth:25 MouseArea { anchors.fill: parent onPressed: rect.Layout.preferredWidth=50 } } Rectangle { color:"blue" Layout.fillHeight: true Layout.fillWidth: true Layout.preferredWidth:25 Rectangle { color:"yellow" width:20 height: 20 radius: 10 property int gx:parent.mapToGlobal((Qt.point(x,y))).x onGxChanged: console.debug("gx changed.") } } } }