Qt widget position

I have a main window with a grid layout and has 8 buttons in 2 lines.

--------------------- | | | 1 2 3 4 | | | | | | 5 6 7 8 | | | --------------------- 

I am trying to show a popup dialog next to the button that was clicked. So, I'm trying to get the coordinates of a button in a slot connected to a clicked() signal.

I tried

 QPoint p = btn->pos(); 

and

 QPoint p = btn->geometry().topLeft(); 

and both of them (0, 0) for some reason. How can I get the position of the button pressed in this slot?

thanks

+4
source share
1 answer

I think you need to display the position of the button relative to the parent window.

QWidget :: mapTo

Translates the widget pos coordinate into the Parent coordinate system. The parent must not be 0 and must be the parent of the widget call.

+5
source

All Articles