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



  • 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.



  • 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?



  • 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);
    @



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.