Get Screen-Coordinates of a QuickControl

RefMa77

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?

Mitch

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get object coordinates from screen coordinates?

From Dev

Get the center coordinates of phone screen

From Dev

Get the center coordinates of phone screen

From Dev

Get coordinates (screen) of GridLayout items

From Dev

How to get world coordinates from screen coordinates in Vispy

From Dev

Get screen coordinates of a node in javaFX 8

From Dev

Get Canvas X and Y coordinates and show on screen

From Dev

Get the coordinates at the edge of the screen from a given angle

From Dev

Get the coordinates of visible part of screen when zoomed in

From Dev

How to get coordinates of touch screen points in ogl?

From Dev

Get the coordinates at the edge of the screen from a given angle

From Dev

How can I get a component's position on screen (screen coordinates)

From Dev

Tool to easily select a pixel on screen and get color and absolute coordinates

From Dev

QML-maps: get coordinates when tap the screen

From Dev

Tool to easily select a pixel on screen and get color and absolute coordinates

From Dev

Delphi, TTreeView: how to get the screen coordinates of the given node and its icon?

From Dev

How to get screen coordinates of view inside of keyboard accessory view?

From Dev

Transformation the screen coordinates to stage coordinates

From Dev

QML Screen Coordinates of Component

From Dev

Screen coordinates in R

From Dev

Display touch coordinates on screen

From Dev

Mapping coordinates to screen in PyGame

From Dev

How to convert workspace coordinates to screen coordinates?

From Dev

How to convert screen coordinates to control coordinates

From Dev

screen coordinates to sharpdx device context coordinates

From Dev

How to convert workspace coordinates to screen coordinates?

From Dev

How do I get my mouse coordinates relative to the window rather than the screen?

From Dev

How do I get the screen coordinates of origin after a bunch of transforms P5.js

From Dev

Get screen coordinates and size of OpenGL 3D Object after transformation

Related Related

  1. 1

    How to get object coordinates from screen coordinates?

  2. 2

    Get the center coordinates of phone screen

  3. 3

    Get the center coordinates of phone screen

  4. 4

    Get coordinates (screen) of GridLayout items

  5. 5

    How to get world coordinates from screen coordinates in Vispy

  6. 6

    Get screen coordinates of a node in javaFX 8

  7. 7

    Get Canvas X and Y coordinates and show on screen

  8. 8

    Get the coordinates at the edge of the screen from a given angle

  9. 9

    Get the coordinates of visible part of screen when zoomed in

  10. 10

    How to get coordinates of touch screen points in ogl?

  11. 11

    Get the coordinates at the edge of the screen from a given angle

  12. 12

    How can I get a component's position on screen (screen coordinates)

  13. 13

    Tool to easily select a pixel on screen and get color and absolute coordinates

  14. 14

    QML-maps: get coordinates when tap the screen

  15. 15

    Tool to easily select a pixel on screen and get color and absolute coordinates

  16. 16

    Delphi, TTreeView: how to get the screen coordinates of the given node and its icon?

  17. 17

    How to get screen coordinates of view inside of keyboard accessory view?

  18. 18

    Transformation the screen coordinates to stage coordinates

  19. 19

    QML Screen Coordinates of Component

  20. 20

    Screen coordinates in R

  21. 21

    Display touch coordinates on screen

  22. 22

    Mapping coordinates to screen in PyGame

  23. 23

    How to convert workspace coordinates to screen coordinates?

  24. 24

    How to convert screen coordinates to control coordinates

  25. 25

    screen coordinates to sharpdx device context coordinates

  26. 26

    How to convert workspace coordinates to screen coordinates?

  27. 27

    How do I get my mouse coordinates relative to the window rather than the screen?

  28. 28

    How do I get the screen coordinates of origin after a bunch of transforms P5.js

  29. 29

    Get screen coordinates and size of OpenGL 3D Object after transformation

HotTag

Archive