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

2

13

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 make a widget transparent for mouse events like this:

QWidget w;
w.setAttribute( Qt::WA_TransparentForMouseEvents );

But this also disables the delivery of mouse events to its children! I want the children of the front widget and the widgets behind the front widget to receive the mouse events.

Qt::WA_TransparentForMouseEvents: When enabled, this attribute disables the delivery of mouse events to the widget and its children. Mouse events are delivered to other widgets as if the widget and its children were not present in the widget hierarchy; mouse clicks and other events effectively “pass through” them. This attribute is disabled by default.

If you have any idea about how to make a widget transparent for mouse events but not it's children then please share!

Overbearing answered 9/1, 2015 at 6:39 Comment(0)
O
15

At last I found a solution :)

QWidget::setMask ( const QRegion & region )

https://doc.qt.io/qt-5/qwidget.html#setMask-1

http://qt-project.org/doc/qt-4.8/qwidget.html#setMask

I found the solution here: http://www.qtcentre.org/archive/index.php/t-3033.html

QRegion reg(frameGeometry());
reg -= QRegion(geometry());
reg += childrenRegion();
setMask(reg);

Now children of the front widget and the widgets behind the front widget respond to the mouse events as required!

Remember, you would need to call these lines again whenever the front widget is re-sized to recalculate the geometry for the mask!

void someWidget::resizeEvent(QResizeEvent *e){
  QWidget::resizeEvent(e);
  QRegion reg(frameGeometry());
  reg-=QRegion(geometry()); 
  reg+=childrenRegion();
  setMask(reg);
}
Overbearing answered 9/1, 2015 at 7:31 Comment(1)
hey this is what i was looking for, but im in PYTHON!, im trying to translate your example to pyqt4, any idea ?Schiro
C
4

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);
}
Chiaroscuro answered 4/10, 2017 at 14:3 Comment(1)
This only functions if it is assumed that the widget behind the transparent widget is its parent . The event will not propagate to a sibling like it would if WA_TransparentForMouseEvents were set.Impolicy

© 2022 - 2024 — McMap. All rights reserved.