Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Moving a 'flying' widget to the position of a QPushButton;mapTo doesnt work as it should
Qt 6.11 is out! See what's new in the release blog

Moving a 'flying' widget to the position of a QPushButton;mapTo doesnt work as it should

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 307 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VogelPapaFinn
    wrote on last edited by
    #1

    Hi!
    Im programming a QWidget (im going to call it MENU) that acts as a context menu (I know about QMenu but there are some problems bc its a top-level widget). To make it easy its the child of a QWidget (TABLE). In this Table there is another Widget (DATA). In there is a QPushButton. When the button is pressed the MENU should be moved to the position of the button. To do that in the slot-function I cast the sender to a QPushButton (I know that this isnt beautiful but this is its only purpose so its fine) and use its sender->pos() to get the position. But because its parent is DATA I need to convert it. To do that Im using sender->mapTo(sender->parentWidget()->parentWidget(), sender->pos()). This should convert the pos relative to DATA to its parent TABLE so MENU can use it.
    But the outcome is... really weird. It looks like its returning two times the size of the x-axes. First im in Windowed mode and press the three dots button:
    ebc599a5-5a0e-4bcd-a6d4-df86030ebd8d-image.png
    As you can see nothing happens. But if I maximize the window now I get the following:
    8377304a-2548-4db5-9652-ce0f90b49921-image.png
    There you can see the MENU at the right position but if I now press the button again it disappears. Via debugging I found out that the x value doubled and its now at ~3k. Im really confused because its always the same depending on the position/size of the window.

    Any idea what Im doing wrong? Just a small advice: If I set the position this->setGeometry(0, 0 [...]) to the top left corner its at the correct spot.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      You've mixed coordinate spaces.

      sender->pos() is the position of sender in parent's coordinates, so you should map them in those coordinates i.e. sender->parentWidget()->mapTo(...
      Widget's position in its own coordinates is always 0x0, so alternatively you can map 0x0 in sender's coordinates sender->mapTo(..., QPoint(0,0)).
      Both should give you the same result.

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved