Results 1 to 2 of 2

Thread: How to get know the position of the widget within its parent widget

  1. #1
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to get know the position of the widget within its parent widget

    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?

    Qt Code:
    1. int findPosition(QWidget *wdg)
    2. {
    3. static QPoint myPos;
    4. if (wdg->parent())
    5. {
    6. myPos += wdg->parent()->pos();
    7. findPosition(wdg->parent());
    8. }
    9. else
    10. return myPos;
    11. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance, Danilo


    I've found a simple solution, as always happen

    Qt Code:
    1. QPoint globalPos = ui->myWidget->mapToGlobal(ui->myWidget->rect().topLeft());
    To copy to clipboard, switch view to plain text mode 

    and it works!
    Last edited by cydside; 11th July 2011 at 15:04. Reason: updated contents

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Smile Re: How to get know the position of the widget within its parent widget

    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.

Similar Threads

  1. Replies: 1
    Last Post: 11th March 2011, 19:34
  2. child widget resize to parent widget
    By bobFromAccounting in forum Newbie
    Replies: 10
    Last Post: 11th February 2011, 02:53
  3. Replies: 7
    Last Post: 14th January 2010, 08:47
  4. Replies: 4
    Last Post: 3rd October 2009, 08:19
  5. How to move child widget with parent widget?
    By anupamgee in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2009, 15:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.