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. Custome Menu in Qmouseevent (Please review)
Qt 6.11 is out! See what's new in the release blog

Custome Menu in Qmouseevent (Please review)

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.2k Views 3 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    I have implemented custom menu in mouse event on Right button . Following is the code for the same . Can some let me know if there is better way to solve the problem

    void myTableView::mousePressEvent(QMouseEvent* event) {

    QTableView::mousePressEvent(event);

    if (event->button() == Qt::RightButton) {
    showContextMenu(event);
    }
    }

    /* Right click menu Copy*/
    void myTableView::showContextMenu(QMouseEvent* event)
    {
    QMenu *menu= new QMenu(this);
    menu->addAction(QObject::tr(Menu"), this, SLOT(func()));
    menu->exec(event->globalPos());
    }

    void myTableView:func() {

    }

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      There are also event for it
      http://www.setnode.com/blog/right-click-context-menus-with-qt/

      myWidget->setContextMenuPolicy(Qt::CustomContextMenu);
      connect(myWidget, SIGNAL(customContextMenuRequested(const QPoint&)),
      this, SLOT(ShowContextMenu(const QPoint&)));

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #3

        @Qt-Enthusiast said:

        void myTableView::mousePressEvent(QMouseEvent* event) {

        QTableView::mousePressEvent(event);

        if (event->button() == Qt::RightButton) {
        showContextMenu(event);
        }
        }

        /* Right click menu Copy*/
        void myTableView::showContextMenu(QMouseEvent* event)
        {
        QMenu *menu= new QMenu(this);
        menu->addAction(QObject::tr(Menu"), this, SLOT(func()));
        menu->exec(event->globalPos());
        }

        void myTableView:func() {

        }

        One question do you see any issue in my implementation ? is there is any issue/problem in the code

        void myTableView::mousePressEvent(QMouseEvent* event) {

        QTableView::mousePressEvent(event);

        if (event->button() == Qt::RightButton) {
        showContextMenu(event);
        }
        }

        /* Right click menu Copy*/
        void myTableView::showContextMenu(QMouseEvent* event)
        {
        QMenu *menu= new QMenu(this);
        menu->addAction(QObject::tr(Menu"), this, SLOT(func()));
        menu->exec(event->globalPos());
        }

        void myTableView:func() {

        }

        about an hour ago Reputation: 0 | Posts: 82

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Well , you create the menu on each showContextMenu
          so its a memory leak
          after exec()
          u should delete

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            Qt Enthusiast
            wrote on last edited by
            #5

            I think what you say is like this

            /* Right click menu Copy*/
            void myTableView::showContextMenu(QMouseEvent* event)
            {
            QMenu *menu= new QMenu(this);
            menu->addAction(QObject::tr(Menu"), this, SLOT(func()));
            menu->exec(event->globalPos());
            delete menu;

            }

            Also do we need to put event-accept like as shown in the code below

            void myTableView::mousePressEvent(QMouseEvent* event) {
            QTableView::mousePressEvent(event);
            if (event->button() == Qt::RightButton) {
            showContextMenu(event);
            }
            }
            /* Right click menu Copy*/
            void myTableView::showContextMenu(QMouseEvent* event)
            {
            QMenu *menu= new QMenu(this);
            menu->addAction(QObject::tr(Menu"), this, SLOT(func()));
            menu->exec(event->globalPos());
            delete menu;
            event->accept
            }
            void myTableView:func() {
            }

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              To add to @mrjj, looking at the lifetime of your menu there's no need to allocate it on the heap, just do it on the stack so it will get destroyed automatically when the function ends.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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