Moving a 'flying' widget to the position of a QPushButton;mapTo doesnt work as it should
-
Hi!
Im programming a QWidget (im going to call it MENU) that acts as a context menu (I know about QMenu but there are some problems bc its a top-level widget). To make it easy its the child of a QWidget (TABLE). In this Table there is another Widget (DATA). In there is a QPushButton. When the button is pressed the MENU should be moved to the position of the button. To do that in the slot-function I cast the sender to a QPushButton (I know that this isnt beautiful but this is its only purpose so its fine) and use itssender->pos()
to get the position. But because its parent is DATA I need to convert it. To do that Im usingsender->mapTo(sender->parentWidget()->parentWidget(), sender->pos())
. This should convert the pos relative to DATA to its parent TABLE so MENU can use it.
But the outcome is... really weird. It looks like its returning two times the size of the x-axes. First im in Windowed mode and press the three dots button:
As you can see nothing happens. But if I maximize the window now I get the following:
There you can see the MENU at the right position but if I now press the button again it disappears. Via debugging I found out that the x value doubled and its now at ~3k. Im really confused because its always the same depending on the position/size of the window.Any idea what Im doing wrong? Just a small advice: If I set the position
this->setGeometry(0, 0 [...])
to the top left corner its at the correct spot. -
You've mixed coordinate spaces.
sender->pos()
is the position of sender in parent's coordinates, so you should map them in those coordinates i.e.sender->parentWidget()->mapTo(...
Widget's position in its own coordinates is always 0x0, so alternatively you can map 0x0 in sender's coordinatessender->mapTo(..., QPoint(0,0))
.
Both should give you the same result.