2

I have to set x,y coordinates of a QWindow. This QWindow has to get the screen coordinates of a QuickControl in my MainWindow + myValue.

How do I get the global Screen-Coordinates for a QuickControl in QML?

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
RefMa77
  • 283
  • 2
  • 14

3 Answers3

4

As @BaCaRoZzo mentioned, use the mapToItem()/mapFromItem() functions:

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
        }
    }
}

As mentioned in the documentation for 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.

That gives us windowPos. To get the position of the control relative to the screen itself, we just add the x and y position of the window.


After a chat with OP, it's clear that he wants to do this in C++. The same principles apply, and in C++ we have more convenient access to the window:

class Control : public QQuickItem
{
    Q_OBJECT
public:
    Control() {}
    ~Control() {}

public slots:
    void printGlobalPos() {
        qDebug() << mapToItem(Q_NULLPTR, QPointF(0, 0)) + window()->position();
    }
};

Register the type:

qmlRegisterType<Control>("Types", 1, 0, "Control");

Use it in 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"
        }
    }
}
Community
  • 1
  • 1
Mitch
  • 23,716
  • 9
  • 83
  • 122
  • Thanks, now I´m able to read relativ the x,y coordinates to the main window. For the global coordiantes I tried to add with Window.x & Window.y but it didn´t work, could you give me an advice? – RefMa77 Sep 09 '15 at 07:02
  • Run the example I provided and drag the window around - the global coordinates will update. Have you tried that? – Mitch Sep 09 '15 at 07:03
  • This QuickControl should be generic, so I can´t use any IDs, can I? – RefMa77 Sep 09 '15 at 07:14
  • Sorry, I don't understand you. Which control are you talking about? – Mitch Sep 09 '15 at 07:18
  • I´m sorry for the unclear description, now I need the global Position of a QComboBox. I can get the x,y Coordinates relativ to the root element with `mapToItem(null,0,0)`. If I understand you correctly, I have to add the global x,y Coordinates of the root Element, but how do I do that? – RefMa77 Sep 09 '15 at 07:34
  • What is your root element? If it's not a `Window` or `ApplicationWindow`, you'll need to find the window and add its `x` and `y` position to get the global position. – Mitch Sep 09 '15 at 07:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89131/discussion-between-refma77-and-mitch). – RefMa77 Sep 09 '15 at 07:37
1

For the x and y coordinates are relative to the parent for all the item but the top ones (aka Window), you can get them at least by walking through the parent chain to the main Window, for which those variables indicate the position relative to the Screen.

It's a matter of additions and subtractions during the journey through the parent chain, quite annoying indeed, but I don't know if there exists another solution.

skypjack
  • 49,335
  • 19
  • 95
  • 187
  • 2
    For inner `Item`s there're the `mapToItem`/`mapFromItem` functions. `Screen` should provide some helper function for top level containers. – BaCaRoZzo Sep 08 '15 at 06:52
  • It makes sense. :-) ... It looks to me that you can use `mapToItem` to short the parent chain, but you still need to get the offset to the `Screen` once you have the relative position of the item, for `mapToItem` maps to the most top one (as an example, the `ApplicationWindow`). Am I wrong? – skypjack Sep 08 '15 at 06:53
0

object mapFromGlobal(real x, real y)

Maps the point (x, y), which is in the global coordinate system, to the item's coordinate system, and returns a point matching the mapped coordinate. This QML method was introduced in Qt 5.7.