获取 QuickControl 的屏幕坐标
Get Screen-Coordinates of a QuickControl
我必须设置 QWindow
的 x,y 坐标。这个 QWindow
必须在我的 MainWindow
+ myValue
.
中获取 QuickControl
的屏幕坐标
如何在 QML 中获取 QuickControl
的全局屏幕坐标?
因为 x
和 y
坐标是相对于所有项目的父级的,除了最上面的项目(又名 Window
),你至少可以通过遍历来获得它们父链到主 Window
,这些变量指示相对于 Screen
.
的位置
在父链的过程中加减法,确实很烦人,不知道有没有其他的解决办法。
如@BaCaRoZzo 所述,使用 mapToItem()
/mapFromItem()
函数:
import QtQuick 2.0
import QtQuick.Window 2.0
import QtQuick.Controls 1.0
Window {
id: window
width: 400
height: 400
visible: true
Button {
id: button
text: "Button"
x: 100
y: 100
readonly property point windowPos: button.mapToItem(null, 0, 0)
readonly property point globalPos: Qt.point(windowPos.x + window.x, windowPos.y + window.y)
}
Column {
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: parent.bottom
Text {
text: "Button position relative to window: x=" + button.windowPos.x + " y=" + button.windowPos.y
}
Text {
text: "Button position relative to screen: x=" + button.globalPos.x + " y=" + button.globalPos.y
}
}
}
如documentation中提到的mapToItem()
:
Maps the point (x, y) or rect (x, y, width, height), which is in this item's coordinate system, to item's coordinate system, and returns a point or rect matching the mapped coordinate.
If item is a null value, this maps the point or rect to the coordinate system of the root QML view.
这给了我们 windowPos
。要获得控件相对于屏幕本身的位置,我们只需添加 window.
的 x
和 y
位置
在与 OP 交谈后,很明显他想用 C++ 来做这件事。同样的原则也适用,在 C++ 中我们可以更方便地访问 window:
class Control : public QQuickItem
{
Q_OBJECT
public:
Control() {}
~Control() {}
public slots:
void printGlobalPos() {
qDebug() << mapToItem(Q_NULLPTR, QPointF(0, 0)) + window()->position();
}
};
注册类型:
qmlRegisterType<Control>("Types", 1, 0, "Control");
在 QML 中使用它:
import QtQuick 2.0
import QtQuick.Window 2.0
import Types 1.0
Window {
id: window
width: 400
height: 400
visible: true
Control {
id: button
x: 100
y: 100
width: 100
height: 40
MouseArea {
anchors.fill: parent
onClicked: button.printGlobalPos()
}
Rectangle {
anchors.fill: parent
color: "transparent"
border.color: "darkorange"
}
}
}
object mapFromGlobal(real x, real y)
将全局坐标系中的点 (x, y) 映射到项目的坐标系,并且 returns 匹配映射坐标的点。
Qt 5.7中引入了这种QML方法。
我必须设置 QWindow
的 x,y 坐标。这个 QWindow
必须在我的 MainWindow
+ myValue
.
QuickControl
的屏幕坐标
如何在 QML 中获取 QuickControl
的全局屏幕坐标?
因为 x
和 y
坐标是相对于所有项目的父级的,除了最上面的项目(又名 Window
),你至少可以通过遍历来获得它们父链到主 Window
,这些变量指示相对于 Screen
.
在父链的过程中加减法,确实很烦人,不知道有没有其他的解决办法。
如@BaCaRoZzo 所述,使用 mapToItem()
/mapFromItem()
函数:
import QtQuick 2.0
import QtQuick.Window 2.0
import QtQuick.Controls 1.0
Window {
id: window
width: 400
height: 400
visible: true
Button {
id: button
text: "Button"
x: 100
y: 100
readonly property point windowPos: button.mapToItem(null, 0, 0)
readonly property point globalPos: Qt.point(windowPos.x + window.x, windowPos.y + window.y)
}
Column {
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: parent.bottom
Text {
text: "Button position relative to window: x=" + button.windowPos.x + " y=" + button.windowPos.y
}
Text {
text: "Button position relative to screen: x=" + button.globalPos.x + " y=" + button.globalPos.y
}
}
}
如documentation中提到的mapToItem()
:
Maps the point (x, y) or rect (x, y, width, height), which is in this item's coordinate system, to item's coordinate system, and returns a point or rect matching the mapped coordinate.
If item is a null value, this maps the point or rect to the coordinate system of the root QML view.
这给了我们 windowPos
。要获得控件相对于屏幕本身的位置,我们只需添加 window.
x
和 y
位置
在与 OP 交谈后,很明显他想用 C++ 来做这件事。同样的原则也适用,在 C++ 中我们可以更方便地访问 window:
class Control : public QQuickItem
{
Q_OBJECT
public:
Control() {}
~Control() {}
public slots:
void printGlobalPos() {
qDebug() << mapToItem(Q_NULLPTR, QPointF(0, 0)) + window()->position();
}
};
注册类型:
qmlRegisterType<Control>("Types", 1, 0, "Control");
在 QML 中使用它:
import QtQuick 2.0
import QtQuick.Window 2.0
import Types 1.0
Window {
id: window
width: 400
height: 400
visible: true
Control {
id: button
x: 100
y: 100
width: 100
height: 40
MouseArea {
anchors.fill: parent
onClicked: button.printGlobalPos()
}
Rectangle {
anchors.fill: parent
color: "transparent"
border.color: "darkorange"
}
}
}
object mapFromGlobal(real x, real y)
将全局坐标系中的点 (x, y) 映射到项目的坐标系,并且 returns 匹配映射坐标的点。 Qt 5.7中引入了这种QML方法。