Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Propagate mouse clicks through a transparent widget



  • I'm trying to create a widget which, when shown, it will intercept any mouse clicks, process them, but then forward the click to the widget that was under the mouse when it was clicked. I've created a class that represents this widget; all it does for now is capture mouse release events, hide itself and post the event to the widget the click was intended for using QApplication::postEvent:

    class Overlay : public QWidget
    {
        Q_OBJECT
    public:
    
        Overlay(QWidget* parent)
            : QWidget(parent){}
    
    protected:
    
        void mouseReleaseEvent(QMouseEvent* event)
        {
            hide();
            auto child = parentWidget()->childAt(event->pos()); // get the child widget the event was intended for
            auto e = new QMouseEvent(*event);
            if(child) QCoreApplication::postEvent(child, e); // why doesn't this have any effect?
            event->accept();
        }
    
        void paintEvent(QPaintEvent* event)
        {
            QPainter p(this);
            p.fillRect(rect(), QColor(0,0,0,150));
        }
    };
    

    Unfortunately, the call to postEvent doesn't seem to have any effect at all. Here is an example where I'm setting up some child widgets; clicking on any widget will print something out:

    class Widget : public QWidget
    {
        Q_OBJECT
    
    public:
    
        Widget(QWidget *parent = nullptr)
        {
            m_overlay = new Overlay(this);
            m_overlay->hide();
    
            auto button = new QPushButton("Add overlay");
            auto button2 = new QPushButton("PushButton");
            auto list = new QListWidget();
            list->addItems(QStringList() << "item1" << "item2" << "item3");
    
            connect(list, &QListWidget::itemClicked, [](const QListWidgetItem* item)
            {
               qDebug() << item->text();
            });
    
            connect(button2, &QPushButton::clicked, []()
            {
                qDebug() << "Button clicked";
            });
    
            connect(button, &QPushButton::clicked, [this]()
            {
                m_overlay->setGeometry(rect());
                m_overlay->raise();
                m_overlay->show();
            });
    
            auto layout = new QVBoxLayout(this);
            layout->addWidget(list);
            layout->addWidget(button);
            layout->addWidget(button2);
        }
    
    public:
    
        ~Widget(){}
    
    private:
    
        Overlay* m_overlay;
    };
    

    I've confirmed that clicks on the overlay are captured and the overlay is hidden as intended but the event is not propagated to the underlying widget. Using QApplication::sendEvent doesn't work either. What am I missing?

    Using Qt 5.15.1 on MacOS 11.2.3. Thanks



  • @linuxfever said in Propagate mouse clicks through a transparent widget:

    event->accept();

    @linuxfever comment the event->accept(); line and test it, whether does this events propagates further.


Log in to reply