How to disable the delivery of mouse events to the widget but not its children in Qt?

前端 未结 2 1268
悲哀的现实
悲哀的现实 2021-02-04 08:53

For the last two days, I\'ve been searching for a way to pass mouse events to the widgets behind a widget used as a container/parent for it\'s children. I know there is a way to

2条回答
  •  猫巷女王i
    2021-02-04 09:04

    The solution of the OP is awesome and very elegant. Just for completeness, another option would be to ignore mouse events when they reach the container widget. It can be done either by sub-classing or through a eventFilter.

    This solution can be helpful if the widget hides/shows many children widgets dynamically and computing the mask becomes difficult.

    Note: In the case you want to track mouse-move events in background widgets, you'd have to setMouseTracking(true) to receive (and then ignore) the QEvent::MouseMove when no button is pressed.

    Example by sub-classing

    ContainerWidget::ContainerWidget(...) {
      setMouseTracking(true);
    }
    
    void ContainerWidget::mouseMoveEvent(QMouseEvent* e) {
      e->ignore();
    }
    void ContainerWidget::mousePressEvent(QMouseEvent* e) {
      e->ignore();
    }
    

    Example using the event filter

    // Assume the container widget is configured in the constructor
    MainWindow::MainWindow(...) {
      // ...
      containerWidget->installEventFilter(this);
      containerWidget->setMouseTracking(true);
      // ...
    }
    
    bool MainWindow::eventFilter(QObject* o, QEvent* e) {
      if (o == containerWidget &&
         (e->type() == QEvent::MouseMove || e->type() == QEvent::MouseButtonPress)) {
        e->ignore();
        return false;
      }
      return QMainWindow::eventFilter(o, e);
    }
    

提交回复
热议问题