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)

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.
  • G Offline
    G Offline
    gde23
    wrote on 1 Apr 2021, 10:41 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?

    P 1 Reply Last reply 1 Apr 2021, 10:58
    0
    • G gde23
      1 Apr 2021, 10:41

      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?

      P Offline
      P Offline
      Pl45m4
      wrote on 1 Apr 2021, 10:58 last edited by Pl45m4 4 Jan 2021, 10:59
      #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
      • G Offline
        G Offline
        gde23
        wrote on 1 Apr 2021, 11:04 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)

        P 1 Reply Last reply 1 Apr 2021, 11:14
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 1 Apr 2021, 11:07 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
          • G gde23
            1 Apr 2021, 11:04

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

            P Offline
            P Offline
            Pl45m4
            wrote on 1 Apr 2021, 11:14 last edited by Pl45m4 4 Jan 2021, 11:31
            #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

            G 1 Reply Last reply 2 Apr 2021, 10:09
            1
            • P Pl45m4
              1 Apr 2021, 11:14

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

              G Offline
              G Offline
              gde23
              wrote on 2 Apr 2021, 10:09 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

              1/6

              1 Apr 2021, 10:41

              • Login

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