QML mapToGlobal() relative to screen or application window

Viewed 2504

I have encountered some strange behaviour.

An item's mapToGlobal() or mapFromGlobal() method, does not seem to be consistent in regard to what the coordinates is relative to.

For some items the position is relative to the application window.

For other item the position is relative to my screen. If I move the application window on the screen they will be different.

Below is a simplification example of my code (I actually have multiple components and signals to decide which component that should be loaded).

MyType.qml:

Item{
  Button{
    onClicked: {
      loader.sourceComponent = component; // Different positions
    }
  }
  Loader{
    id: loader
    anchors.fill: parent
  }

  Component{
    id: component
    Rectangle{
      Component.onCompleted: {
        var point = mapFromGlobal(0, 0);
        console.log("temp 2: ", point.x, point.y);
      }
    }
  }

  Component.onCompleted: {
    //loader.sourceComponent = component; // Same positions
    var point = mapFromGlobal(0, 0);
    console.log("temp 1: ", point.x, point.y);
  }
}

main.qml

ApplicationWindow {
  id: appWindow

  visible: true

  width: 600
  height: 400

  MyType{
    anchors.fill: parent
  }
}

The resulting output is

temp 1: 0 0

temp 2: -199 -85

Does anyone know why the positions are sometimes relative to screen and sometimes the application window? Or knows of another explanation for this strange behaviour?

Edit:

If I load the component directly in Component.onCompleted (outcommented in sample code), then both outputs are 0 0. Unfortunately this didn't get me closer to an explanation, except it has something to do with the Loader elements.

0 Answers
Related