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. QTreeView & mapToGlobal(...): Is this considered a bug?

QTreeView & mapToGlobal(...): Is this considered a bug?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 5.1k Views 1 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.
  • L Offline
    L Offline
    LiamMaru
    wrote on last edited by
    #1

    I've encountered this a few times, it's a bit inconvenient but there's a simple workaround. Consider the following:

    @
    // Get the global position...
    QPoint globalPos = FileTreeWidget::mapToGlobal(event->pos());

    // Build and show a context menu.
    QMenu* defaultContextMenu = new QMenu();
    defaultContextMenu->addAction(EraseAction);
    defaultContextMenu->addAction(OpenAction);
    defaultContextMenu->exec(globalPos);
    @

    if I have no header shown, this works fine. However, with the top header shown, the above doesn't take into account the height of the tree view header and the context menu is shown slightly offset from the mouse position. The workaround is to add the height of the tree view header to the y coordinate of the global position, and voila.

    My questions are as follows: is there a more efficient function than 'mapToGlobal', to accomplish what I'm doing here, without adding the offset, and is this considered a bug? I couldn't find it in the Qt bug tracker.

    1 Reply Last reply
    1
    • D Offline
      D Offline
      Darryl DSouza
      wrote on last edited by
      #2

      mapToGlobal translates the widget(your file tree) coordinate position to global screen coordinates. I do not see any other way to get this position.

      On what event are you showing the menu?

      Darryl D'Souza

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        Hi LiamMaru,

        I think your ptoblem is another one. The event is sent to the viewport widget of the FileTreeWidget.
        So this could help:

        @
        // Get the global position...
        QPoint globalPos = FileTreeWidget::viewport()->mapToGlobal(event->pos());

        // Build and show a context menu.
        QMenu* defaultContextMenu = new QMenu();
        defaultContextMenu->addAction(EraseAction);
        defaultContextMenu->addAction(OpenAction);
        defaultContextMenu->exec(globalPos);
        @

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        1
        • L Offline
          L Offline
          LiamMaru
          wrote on last edited by
          #4

          Gerolf, nail on the head! Thanks for that, seems to have been a simple oversight, I'll keep that in mind for the future.

          1 Reply Last reply
          1

          • Login

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