PDA

View Full Version : How to get know the position of the widget within its parent widget



cydside
11th July 2011, 15:00
Hi to all,
I'm looking for a method to calculate a widget position nested in an unknown structure of parent widgets. Should I use a recursive function like the follow pseudo-code?



int findPosition(QWidget *wdg)
{
static QPoint myPos;
if (wdg->parent())
{
myPos += wdg->parent()->pos();
findPosition(wdg->parent());
}
else
return myPos;
}


Thanks in advance, Danilo


I've found a simple solution, as always happen :)



QPoint globalPos = ui->myWidget->mapToGlobal(ui->myWidget->rect().topLeft());


and it works!

yogeshgokul
11th April 2012, 07:45
Thank you for posting answer!!!!

I faced this issue:
When we place a widget inside several layers of widgets or frames then the pos() method of the inside widget starts returning wrong value. But your solution solves even that problem.
I thought that using pos() or rect().topLeft() are same but practically they are not.