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. Pop-up menu only appears on screen top left corner

Pop-up menu only appears on screen top left corner

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 4.0k Views
  • 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.
  • S Offline
    S Offline
    SDrake
    wrote on last edited by
    #1

    Hello, very new to this. If someone could please point me the right direction (or offer a solution) it would be much appreciated.

    I'm using Qt Creator to create the ui of an application. Using mingw on Windows 7 os... I do not have any MS software with compilers.

    Within the application there is a modified tree view widget where I am trying to have a context menu appear when right-clicked... and at the location of the click. So far - with one exception - the menu always appears at the top left corner of my computer screen regardless of where the application appears or is moved. The one exception is when the menu coordinates are hard coded. How to make it appear where the mouse is clicked?

    Code below:

    @#include "modqtreeview.h"
    #include <QMouseEvent>
    #include <QMenu>
    #include <QPoint>

    modQTreeView::modQTreeView(QWidget *parent) : QTreeView(parent)
    {
    installEventFilter(this);
    }

    bool modQTreeView::eventFilter(QObject *obj, QEvent *event)
    {
    if(event->type() == QEvent::ContextMenu)
    {
    QMouseEvent mouseEvent = static_cast<QMouseEvent> (event);
    QMenu *menu = new QMenu(this);
    menu->addAction(new QAction("New",this));
    menu->addAction(new QAction("Edit",this));
    menu->addAction(new QAction("Delete",this));
    //QPoint xy = QPoint(mouseEvent->pos().x(), mouseEvent->pos().y()); // same top left corner with this
    //QPoint xy = QPoint(200, 500); // position hard coding worked
    //menu->move(xy);
    menu->pos() = mouseEvent->globalPos();
    menu->exec();
    return false;
    }
    else
    return QTreeView::eventFilter(obj, event);
    }@

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Seamus Boyle
      wrote on last edited by
      #2

      @
      // replace these two lines
      menu->pos() = mouseEvent->globalPos();
      menu->exec();
      // with this
      menu->exec(event->globalPos());
      @

      @
      // This line does nothing, pos() is for reading
      menu->pos() = mouseEvent->globalPos();
      @

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SDrake
        wrote on last edited by
        #3

        Seamus,

        Thank You for the suggestion. The build came back with:

        'class QEvent' has no member named 'globalPos'

        Since then tried:

        @menu->move(mouseEvent->pos());@

        then:

        @menu->move(mouseEvent->globalPos());@

        followed by:

        @menu-> exec();@

        Still showing menu in top left corner of screen when using those.

        Steven

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SDrake
          wrote on last edited by
          #4

          And the solution is......................

          @#include "modqtreeview.h"
          #include <QMouseEvent>
          #include <QMenu>
          #include <QCursor> // added in to use QCursor below

          modQTreeView::modQTreeView(QWidget *parent) : QTreeView(parent)
          {
          installEventFilter(this);
          }

          bool modQTreeView::eventFilter(QObject *obj, QEvent *event)
          {
          if(event->type() == QEvent::ContextMenu)
          {
          QMouseEvent mouseEvent = static_cast<QMouseEvent> (event);
          QMenu *menu = new QMenu(this);
          menu->addAction(new QAction("Add Data",this));
          menu->addAction(new QAction("Add Group",this));
          menu->addAction(new QAction("Add New View",this));
          menu->exec(QCursor::pos()); // makes pop-up menu appear at location where mouse is clicked
          return false;
          }
          else
          return QTreeView::eventFilter(obj, event);
          }@
          A wonderfully simple solution inspired by a posting elsewhere.

          Steven

          1 Reply Last reply
          0

          • Login

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