PDA

View Full Version : Absolute widget coordinates



rbp
27th January 2009, 05:49
The x() and y() functions return the relative coordinates of a widget to its parent. Is there a simple way to find the absolute coordinates of a widget in a window (rather than iterating up parents to the window)?

thanks, Richard

faldzip
27th January 2009, 07:10
you can use QWidget::mapToGlobal(const QPoint & pos) which gives you pos translated to global coordinates

rbp
27th January 2009, 22:45
thanks for the reply faldżip.
Unfortunately mapToGlobal() gives the result in screen coordinates, meaning that it is dependent on the location of the window.

wysota
28th January 2009, 00:40
Try this:

QPoint localPoint = ...
QPoint windowPoint = myWidget->mapTo(myWidget->window(), localPoint);

It's probably equivalent to:

QPoint mapToWindow(QWidget *from, QPoint pt){
QWidget *wnd = from->window();
while(from && from!=wnd){
pt = from->mapToParent(pt);
from = from->parent();
}
return pt;
}

rbp
28th January 2009, 04:30
thanks (again) wysota.