Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Problem overriding mousePressEvent

    General and Desktop
    c++ qt 5.9.5 qlineedit
    2
    6
    1036
    Loading More Posts
    • 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
      Daniel_Contro last edited by

      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 Reply Quote 1
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        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 Reply Quote 2
        • D
          Daniel_Contro @mrjj last edited by

          @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();
            }
          }
          
          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @Daniel_Contro last edited by 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 1 Reply Last reply Reply Quote 2
            • D
              Daniel_Contro @mrjj last edited by

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

              mrjj 1 Reply Last reply Reply Quote 1
              • mrjj
                mrjj Lifetime Qt Champion @Daniel_Contro last edited by

                @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 Reply Quote 1
                • First post
                  Last post