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

前端 未结 2 1263
悲哀的现实
悲哀的现实 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条回答
  •  情书的邮戳
    2021-02-04 09:16

    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);
    }
    

提交回复
热议问题