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. Problem overriding mousePressEvent
Qt 6.11 is out! See what's new in the release blog

Problem overriding mousePressEvent

Scheduled Pinned Locked Moved Solved General and Desktop
c++qt 5.9.5qlineedit
6 Posts 2 Posters 3.5k 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.
  • D Offline
    D Offline
    Daniel_Contro
    wrote on last edited by
    #1

    Hi to everyone,
    I've a custom widget derived from QLineEdit and I'm trying to overload the mousePressEvent to show a popup menu if I press the mouse right button. So far my code is the following:

    void TaskPreview::mousePressEvent(QMouseEvent *event) {
      if (event->button() == Qt::MouseButton::RightButton) {
        _actions->addAction(_moveLeft);
        _actions->addAction(_moveRight);
        _actions->addAction(_delete);
        _actions->popup(QWidget::mapToGlobal(event->pos()));
      }
    }
    

    The menu is correctly rendered, the problem is that on top of it there's another menu and I don't exactly know where it comes from, probably from the original QLineEdit mousePressEvent but I don't know how this is possible, the widget is stored as TaskPreview and has TaskPreview dynamic type.

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

      Hi
      I guess it is the normal context menu.
      To replace it, you should use
      setContextMenuPolicy(Qt::CustomContextMenu);
      and then use the signal that is sent when its time to show it

      connect(thewidget, SIGNAL(customContextMenuRequested(QPoint)),
      SLOT(customMenuRequested(QPoint)));
      and not via mousePressEvent overide

      https://doc.qt.io/qt-5/qwidget.html#customContextMenuRequested

      D 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi
        I guess it is the normal context menu.
        To replace it, you should use
        setContextMenuPolicy(Qt::CustomContextMenu);
        and then use the signal that is sent when its time to show it

        connect(thewidget, SIGNAL(customContextMenuRequested(QPoint)),
        SLOT(customMenuRequested(QPoint)));
        and not via mousePressEvent overide

        https://doc.qt.io/qt-5/qwidget.html#customContextMenuRequested

        D Offline
        D Offline
        Daniel_Contro
        wrote on last edited by
        #3

        @mrjj Thank you for replying, but now I'm wondering where I should define the menu/make it point to a QMenu*. According to what you replied, is it correct to emit the signal customContextMenuRequested(QPoint) in the overrided mousePressEvent, transforming my code as follows?

        void TaskPreview::mousePressEvent(QMouseEvent *event) {
          if (event->button() == Qt::MouseButton::RightButton) {
            emit customMenuRequested(event->pos();
          }
        }
        
        mrjjM 1 Reply Last reply
        0
        • D Daniel_Contro

          @mrjj Thank you for replying, but now I'm wondering where I should define the menu/make it point to a QMenu*. According to what you replied, is it correct to emit the signal customContextMenuRequested(QPoint) in the overrided mousePressEvent, transforming my code as follows?

          void TaskPreview::mousePressEvent(QMouseEvent *event) {
            if (event->button() == Qt::MouseButton::RightButton) {
              emit customMenuRequested(event->pos();
            }
          }
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @Daniel_Contro
          Hi
          Qt will emit the signal. you just need to set to custom menu and hook up to a slot.
          Then Qt should send you the signal when you right click. you should not have override mousePress event as
          then it wont work unless you call base class also.

          D 1 Reply Last reply
          2
          • mrjjM mrjj

            @Daniel_Contro
            Hi
            Qt will emit the signal. you just need to set to custom menu and hook up to a slot.
            Then Qt should send you the signal when you right click. you should not have override mousePress event as
            then it wont work unless you call base class also.

            D Offline
            D Offline
            Daniel_Contro
            wrote on last edited by
            #5

            @mrjj Thanks a lot for the help, it worked. I have another problem with the main window menuBar, may I ask you here or should I open a new thread?

            mrjjM 1 Reply Last reply
            1
            • D Daniel_Contro

              @mrjj Thanks a lot for the help, it worked. I have another problem with the main window menuBar, may I ask you here or should I open a new thread?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Daniel_Contro
              hi
              Good to hear.
              best to set this as solved and make a new one as then its easier for others to search :)

              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