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
QtWS25 Last Chance

Problem overriding mousePressEvent

Scheduled Pinned Locked Moved Solved General and Desktop
c++qt 5.9.5qlineedit
6 Posts 2 Posters 2.4k 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.
  • D Offline
    D Offline
    Daniel_Contro
    wrote on 1 Sept 2020, 15:50 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 1 Sept 2020, 16:58 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 1 Sept 2020, 17:18
      2
      • M mrjj
        1 Sept 2020, 16:58

        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 1 Sept 2020, 17:18 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();
          }
        }
        
        M 1 Reply Last reply 1 Sept 2020, 17:23
        0
        • D Daniel_Contro
          1 Sept 2020, 17:18

          @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();
            }
          }
          
          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 1 Sept 2020, 17:23 last edited by mrjj 9 Jan 2020, 17:24
          #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 1 Sept 2020, 17:44
          2
          • M mrjj
            1 Sept 2020, 17:23

            @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 1 Sept 2020, 17:44 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?

            M 1 Reply Last reply 1 Sept 2020, 17:45
            1
            • D Daniel_Contro
              1 Sept 2020, 17:44

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

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 1 Sept 2020, 17:45 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

              2/6

              1 Sept 2020, 16:58

              topic:navigator.unread, 4
              • Login

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