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. How to close dialog when mouse leaves a QWidget in the mainwindow (but not when it is over the dialog)
QtWS25 Last Chance

How to close dialog when mouse leaves a QWidget in the mainwindow (but not when it is over the dialog)

Scheduled Pinned Locked Moved Solved General and Desktop
leaveeventdialogpopup
6 Posts 3 Posters 1.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.
  • gde23G Offline
    gde23G Offline
    gde23
    wrote on last edited by
    #1

    Hello,

    I have a dialog, that shows at the mouse position and is related to the underlying widget (like a right click menu for the underlying widget)

    myWidget* tool = new myWidget(this);
    myWidget->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
    QPoint p = this->mapToGlobal(QPoint(event->x(), event->y()));
    tool->setGeometry(p.x(), p.y(), 100 , 50);
    tool->show();
    

    Now I want the dialog to close when the mouse leaves the underlying widget. However using leaveEvent() does not work, since as soon as the widgets appears the leave event gets triggered (which is totally what it should do)

    So how can I detect if the mouse moves outside the underlying widget?

    Pl45m4P 1 Reply Last reply
    0
    • gde23G gde23

      Hello,

      I have a dialog, that shows at the mouse position and is related to the underlying widget (like a right click menu for the underlying widget)

      myWidget* tool = new myWidget(this);
      myWidget->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
      QPoint p = this->mapToGlobal(QPoint(event->x(), event->y()));
      tool->setGeometry(p.x(), p.y(), 100 , 50);
      tool->show();
      

      Now I want the dialog to close when the mouse leaves the underlying widget. However using leaveEvent() does not work, since as soon as the widgets appears the leave event gets triggered (which is totally what it should do)

      So how can I detect if the mouse moves outside the underlying widget?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @gde23

      Install an eventFilter on your widget. Let your mainWindow handle the events and close your dialog when the event is a QEvent::Leave type.

      Edit:
      You have to enable mouseTracking first.
      myTool->setMouseTracking(true);


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      1
      • gde23G Offline
        gde23G Offline
        gde23
        wrote on last edited by
        #3

        @Pl45m4: I tried that, but the QEvent::Leave event also gets triggered when I move the mouse over the dialog (which is in front of the widget)

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

          Hi,

          Then apply the reverse logic: if the enter event is not from your dialog or the underlying widget, hide it.

          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
          1
          • gde23G gde23

            @Pl45m4: I tried that, but the QEvent::Leave event also gets triggered when I move the mouse over the dialog (which is in front of the widget)

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #5

            @gde23

            Maybe you can make use of

            • https://doc.qt.io/qt-5/qapplication.html#topLevelAt

            So if you leave your widget at QPointp and p belongs to your dialog, you ignore that event.

            Edit: Probably you dont even need topLevelAt for that.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            gde23G 1 Reply Last reply
            1
            • Pl45m4P Pl45m4

              @gde23

              Maybe you can make use of

              • https://doc.qt.io/qt-5/qapplication.html#topLevelAt

              So if you leave your widget at QPointp and p belongs to your dialog, you ignore that event.

              Edit: Probably you dont even need topLevelAt for that.

              gde23G Offline
              gde23G Offline
              gde23
              wrote on last edited by
              #6

              Thanks for the ideas, I got it working like this now:

              bool myWidget::event(QEvent* e) 
              {
                  if(e->type() == QEvent::Leave) 
                  {
                      QPoint view_pos(x(), y());
                      QPoint view_pos_global = mapToGlobal(view_pos);
                      QPoint mouse_global = QCursor::pos();
                      if(mouse_global.x() < view_pos_global.x() || mouse_global.x() > view_pos_global.x() + width())
                      {
                          closeMenu();
                      }
                      else if(mouse_global.y() < view_pos_global.y() || mouse_global.y() > view_pos_global.y() + height())
                      {
                          closeMenu();
                      }
                  }
                  return QWidget::event(e);
              }
              
              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