Results 1 to 8 of 8

Thread: mapToGlobal/mapFromGlobal isn't working properly

  1. #1
    Join Date
    Apr 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default mapToGlobal/mapFromGlobal isn't working properly

    I am using Qt 4.7 and have come across an interesting problem with positioning widgets using QWidget::mapToGlobal() and QWidget::mapFromGlobal()...

    I have a button (in a dock bar) that displays a widget as a popup. I am trying to position the popup relative to the button in different ways depending on where the button is on the screen (since it is in a dock bar the user can move it around).

    My method for positioning the popup takes a widget to use as the widget to position the popup on. I then get the global coordinates of this widget and the width and height of the popup widget to determine the correct position of the popup.

    What is interesting is that the coordinates for mapToGlobal() and mapFromGlobal are only returning the correct results when in the bottom-right corner of the screen. The other three coordinates return incorrect results and the popup does not display in the correct spot.

    Here is the code for positioning our popup:
    Qt Code:
    1. void DzMainWindow::positionUIPopUp( QWidget* posWgt )
    2. {
    3. if(!posWgt)
    4. return;
    5.  
    6. DzUIPopUpWgt* popupWgt = getUIPopUp();
    7.  
    8. if(!popupWgt)
    9. return;
    10.  
    11. dzApp->processEvents();
    12.  
    13. // Find a parent widget that has an orientation
    14. Qt::Orientation orient = Qt::Horizontal;
    15. if(QWidget* parentWgt = posWgt->parentWidget())
    16. {
    17. do
    18. {
    19. QVariant data = parentWgt->property("orientation");
    20. if(data.isValid())
    21. {
    22. orient = Qt::Orientation(data.value<int>());
    23. break;
    24. }
    25. parentWgt = parentWgt->parentWidget();
    26. } while (parentWgt);
    27. }
    28.  
    29. QPoint posWgtGlobalPos = posWgt->mapToGlobal(QPoint(0,0));
    30.  
    31. // Split screen into quadrants
    32. QDesktopWidget *desktop = QApplication::desktop();
    33. QRect screenRect = desktop->availableGeometry(posWgtGlobalPos);
    34.  
    35. int screenMidX = screenRect.x() + (screenRect.width() / 2);
    36. int screenMidY = screenRect.y() + (screenRect.height() / 2);
    37.  
    38. QRect topLeft = screenRect;
    39. topLeft.setRight(screenMidX);
    40. topLeft.setBottom(screenMidY);
    41.  
    42. QRect topRight = screenRect;
    43. topRight.setLeft(screenMidX);
    44. topRight.setBottom(screenMidY);
    45.  
    46. QRect bottomLeft = screenRect;
    47. bottomLeft.setRight(screenMidX);
    48. bottomLeft.setTop(screenMidY);
    49.  
    50. QRect bottomRight = screenRect;
    51. bottomRight.setLeft(screenMidX);
    52. bottomRight.setTop(screenMidY);
    53.  
    54. // Position the popup widget based on the quadrant posWgt is in
    55. int popX = posWgtGlobalPos.x();
    56. int popY = posWgtGlobalPos.y();
    57.  
    58. if(topLeft.contains( posWgtGlobalPos ))
    59. {
    60. if (orient == Qt::Horizontal)
    61. {
    62. popY += posWgt->height();
    63. }
    64. else
    65. {
    66. popX += posWgt->width();
    67. }
    68. }
    69. else if(topRight.contains( posWgtGlobalPos ))
    70. {
    71. popX -= popupWgt->width();
    72.  
    73. if (orient == Qt::Horizontal)
    74. {
    75. popX += posWgt->width();
    76. popY += posWgt->height();
    77. }
    78. }
    79. else if(bottomRight.contains( posWgtGlobalPos ))
    80. {
    81. popX -= popupWgt->width();
    82. popY -= popupWgt->height();
    83.  
    84. if (orient == Qt::Horizontal)
    85. {
    86. popX += posWgt->width();
    87. }
    88. else
    89. {
    90. popY += posWgt->height();
    91. }
    92. }
    93. else if(bottomLeft.contains( posWgtGlobalPos ))
    94. {
    95. popY -= popupWgt->height();
    96.  
    97. if (orient == Qt::Vertical)
    98. {
    99. popX += posWgt->width();
    100. popY += posWgt->height();
    101. }
    102. }
    103.  
    104. QPoint popPnt = popupWgt->mapFromGlobal(QPoint(popX, popY));
    105. popupWgt->setGeometry(
    106. popupWgt->x() + popPnt.x(),
    107. popupWgt->y() + popPnt.y(),
    108. popupWgt->width(),
    109. popupWgt->height());
    110.  
    111. //popupWgt->move(popupWgt->x() + popPnt.x(), popupWgt->y() + popPnt.y());
    112. }
    To copy to clipboard, switch view to plain text mode 

    Are we doing something wrong here? I've had two other guys look at this code and we've already spent 3 days trying to figure this out...

    Any tips, pointers, suggestions, would be greatly appreciated

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mapToGlobal/mapFromGlobal isn't working properly

    Does getUIPopUp() create the widget or does it simply return it? Was the widget visible on the screen prior to running mapToGlobal()? What does the following print?
    Qt Code:
    1. qDebug() << getUIPopUp()->geometry() << getUIPopUp()->window()->geometry();
    To copy to clipboard, switch view to plain text mode 

    Are the values what you expect? What is the purpose of the do-while loop?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: mapToGlobal/mapFromGlobal isn't working properly

    Hi Wysota,

    Thanks for the quick reply! Here are the answers to your questions:

    • getUIPopUp() simply returns the widget. It is created previously
    • The call to processEvents() ensures that the widget is visible before moving it
    • The posWgt that is passed in is guaranteed to only live within in a dockbar. The do while loop goes through the parents until it finds a parent that has an orientation (the dock bar). The positioning changes based on the orientation of the dock bar.
    • I'm getting results that I would not expect from the debug code that you gave me... See below for details...


    I placed the debug code just before moving popupWgt and just after moving popupWgt. I ran two test cases, one with posWgt being in the Bottom-Right corner (this works) and one with posWgt being in the Top-Left corner (this doesn't work). However, In both cases I'm getting results that I didn't expect.

    Bottom-Right (WORKS)

    Before setGeometry() or move()
    • getUIPopup()->geometry() //(805, 280, 310x521)
    • getUIPopup()->window()->geometry() //(805, 280, 310x521)

    After setGeometry() or move()
    • getUIPopup()->geometry() //(1524, 456, 314x566)
    • getUIPopup()->window()->geometry() //(1524, 456, 314x566)

    There are two things that are confusing me:
    • The width and height changes after the move
    • The new x,y coordinates are not offset by the values of popPnt: (723, 221)



    Top-Left (BROKEN)

    Before setGeometry() or move()
    • getUIPopup()->geometry() //(805, 280, 310x521)
    • getUIPopup()->window()->geometry() //(805, 280, 310x521)

    After setGeometry() or move()
    • getUIPopup()->geometry() //(736, 74, 314x566)
    • getUIPopup()->window()->geometry() //(736, 74, 314x566)

    There are two things that are confusing me:
    • The width and height changes after the move
    • The new x,y coordinates are not offset by the values of popPnt: (-65, -161)

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mapToGlobal/mapFromGlobal isn't working properly

    Quote Originally Posted by janderson.daz View Post
    The posWgt that is passed in is guaranteed to only live within in a dockbar. The do while loop goes through the parents until it finds a parent that has an orientation (the dock bar). The positioning changes based on the orientation of the dock bar.
    Wouldn't it be safer to ask the widget for its class name? More than one class may have a property called "orientation".

    Is the parent of the widget you are trying to move managed by a layout?

    Finally, what is the ultimate effect you are trying to achieve? Maybe there is a better approach to reach the goal.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Apr 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: mapToGlobal/mapFromGlobal isn't working properly

    Quote Originally Posted by wysota View Post
    Wouldn't it be safer to ask the widget for its class name? More than one class may have a property called "orientation".
    That is a good point. This code was originally written to be as generic as possible, allowing for any widget with an orientation property. This has since been narrowed down to just a dock bar. We will change this code to do as you suggested...

    Quote Originally Posted by wysota View Post
    Is the parent of the widget you are trying to move managed by a layout?
    The parent of the popup widget is DzMainWindow, which is a subclass of QMainWindow, and are not doing our own layout. We use methods such as QMainWindow::setMenuWidget() so we are using the default layout that QMainWindow uses.

    Quote Originally Posted by wysota View Post
    Finally, what is the ultimate effect you are trying to achieve? Maybe there is a better approach to reach the goal.
    We are trying to position the popup widget so that it's corners align with one of the corners of the reference widget (posWgt), based on the orientation of the divider bar and the quadrant that the reference widget.

    Examples:
    posWgt Quadrant: Bottom-Right
    Dock Bar Orientation: Horizontal
    Alignment: popupWgt's bottom-right corner should align with posWgt's top-right corner

    posWgt Quadrant: Bottom-Right
    Dock Bar Orientation: Vertical
    Alignment: popupWgt's bottom-right corner should align with posWgt's top-left corner

    posWgt Quadrant: Top-Left
    Dock Bar Orientation: Horizontal
    Alignment: popupWgt's top-left corner should align with posWgt's bottom-left corner

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: mapToGlobal/mapFromGlobal isn't working properly

    Quote Originally Posted by janderson.daz View Post
    The parent of the popup widget is DzMainWindow, which is a subclass of QMainWindow, and are not doing our own layout. We use methods such as QMainWindow::setMenuWidget() so we are using the default layout that QMainWindow uses.
    So it is managed by a layout which might explain why your own geometry management fails. If you wish to do your own moving/resizing, you can't use layouts at the same time.

    We are trying to position the popup widget so that it's corners align with one of the corners of the reference widget (posWgt), based on the orientation of the divider bar and the quadrant that the reference widget.

    Examples:
    posWgt Quadrant: Bottom-Right
    Dock Bar Orientation: Horizontal
    Alignment: popupWgt's bottom-right corner should align with posWgt's top-right corner

    posWgt Quadrant: Bottom-Right
    Dock Bar Orientation: Vertical
    Alignment: popupWgt's bottom-right corner should align with posWgt's top-left corner

    posWgt Quadrant: Top-Left
    Dock Bar Orientation: Horizontal
    Alignment: popupWgt's top-left corner should align with posWgt's bottom-left corner
    I suggest you forget about docks for now and implement your solution as working for any possible widget. When you have that done you can adjust the code to work for your specific case. mapToGlobal() and mapFromGlobal() both work fine so if they fail in your case then it has to be caused by your own code. Move the popup code out of your specific use-case, make it work as a standalone solution and then integrate it back to your main project.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Apr 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: mapToGlobal/mapFromGlobal isn't working properly

    Quote Originally Posted by wysota View Post
    So it is managed by a layout which might explain why your own geometry management fails. If you wish to do your own moving/resizing, you can't use layouts at the same time.
    That makes sense. However, as a quick test I changed the code that creates the popup so that it doesn't have a parent. Shouldn't that mean that it's position is no longer affected by the main window's layout? When I did this, I didn't see any change in behavior...

    Quote Originally Posted by wysota View Post
    I suggest you forget about docks for now and implement your solution as working for any possible widget. When you have that done you can adjust the code to work for your specific case. mapToGlobal() and mapFromGlobal() both work fine so if they fail in your case then it has to be caused by your own code. Move the popup code out of your specific use-case, make it work as a standalone solution and then integrate it back to your main project.
    I will look into doing this

  8. #8
    Join Date
    Apr 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: mapToGlobal/mapFromGlobal isn't working properly

    Problem has been "solved". It is not a layout issue as was suspected. While performing different tests I noticed a few things that led me to the "fix":
    • The geometry before and after the move was different
    • This caused the final x and y coordinates to be off by a certain amount
    • I noticed that the amount the coordinates were off also happened to coincide directly with the geometry changing (e.g. If the x coordinate was off by 4 pixels from what was expected, the width of the final geometry would change by 4 pixels. The same was true for the y coordinates).


    These things led me to believe that I was calculating my positions based on bad geometry, which would in turn cause my positions to be incorrect... I tried calling updateGeometry() on the popup widget but that didn't have any effect.

    As a test, I tried calling move on the popup widget before calculating my offsets. This turned out to work! So, for some reason that I can't figure out, the geometry is not correct until after the first time the widget has been moved.

    I admit this is a bit hackish but I get the results I want. Everything lines up perfectly now. I am open to other solutions if anyone has any to offer. Until then this is how we are "fixing" the problem.

Similar Threads

  1. Replies: 1
    Last Post: 23rd February 2012, 12:13
  2. QToolbar expanding sizePolicy not working properly
    By sfcheng77 in forum Qt Programming
    Replies: 2
    Last Post: 23rd February 2011, 00:50
  3. memcpy not working properly
    By sattu in forum Qt Programming
    Replies: 3
    Last Post: 27th October 2010, 00:33
  4. ScrollZoomer not working properly..
    By Raghaw in forum Qwt
    Replies: 1
    Last Post: 30th October 2009, 07:51
  5. Replies: 1
    Last Post: 2nd June 2006, 00:54

Tags for this Thread

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.