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. Propagate mouse clicks through a transparent widget
Forum Updated to NodeBB v4.3 + New Features

Propagate mouse clicks through a transparent widget

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 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.
  • L Offline
    L Offline
    linuxfever
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • nageshN Offline
      nageshN Offline
      nagesh
      wrote on last edited by
      #2

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

      1 Reply Last reply
      1

      • Login

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