<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to propagate the wheelEvent to multiple widgets of my centralwidget]]></title><description><![CDATA[<p dir="auto">Hello all,</p>
<p dir="auto">I am making a C++ desktop application with Qt 6.2.4.</p>
<p dir="auto">Basically, I have a MainWindow which has a QVBoxLayout which has 2 custom widgets (let's call them MyWidget which has the wheelEvent function overrided).</p>
<p dir="auto">Code extract:</p>
<pre><code>MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent), ui(new Ui::MainWindow) {
    ui-&gt;setupUi(this);

    QVBoxLayout* vLayout = new QVBoxLayout();
    vLayout-&gt;setSpacing(0);

    MyWidget* widget1 = new MyWidget(this);
    MyWidget* widget2 = new MyWidget(this);
    vLayout-&gt;addWidget(widget1);
    vLayout-&gt;addWidget(widget2);

    QWidget* centralWidget = new QWidget();
    centralWidget-&gt;setLayout(vLayout);

    setCentralWidget(centralWidget);
}
</code></pre>
<p dir="auto">When my mouse is on the first MyWidget and I use the wheel with the key Ctrl pressed, the action is only performed on this widget (the focused one), but I want that all MyWidget receive the event.</p>
<p dir="auto">How to do so ? I try to implement the eventFilter function in the MainWindow class, but I don't succeed on catching the event at the highest level in order to propagate it.</p>
<p dir="auto">Code extract:</p>
<pre><code>bool MainWindow::eventFilter(QObject *obj, QEvent *event) {
    if (event-&gt;type() == QEvent::Wheel) {
        if (QWidget *widget = qobject_cast&lt;QWidget*&gt;(obj); widget &amp;&amp; widget-&gt;layout()) {
            QString test = widget-&gt;objectName();
            QLayout *layout = widget-&gt;layout();
            int nb = layout-&gt;count();
            for (int i{0}; i &lt; layout-&gt;count(); ++i) {
                if (QLayoutItem *item = layout-&gt;itemAt(i); item-&gt;widget()) {
                    QCoreApplication::sendEvent(item-&gt;widget(), event);
                }
            }

            return true;
        }
    }

    return QMainWindow::eventFilter(obj, event);
}
</code></pre>
<p dir="auto">Hope I am enough clear.</p>
]]></description><link>https://forum.qt.io/topic/147463/how-to-propagate-the-wheelevent-to-multiple-widgets-of-my-centralwidget</link><generator>RSS for Node</generator><lastBuildDate>Mon, 21 Sep 2026 09:10:26 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/147463.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 03 Aug 2023 14:12:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to propagate the wheelEvent to multiple widgets of my centralwidget on Mon, 07 Aug 2023 08:20:00 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> In the code I provide in my previous message, the MyWidget seems to execute the code assiocated to the wheelevent, and then the event is propagated to the parent widget (the central widget is this case). And the central widget re-propagate the event to all its children (including the original widget).</p>
<p dir="auto">How to avoid this first process, the one before sending the event to the parent ?</p>
]]></description><link>https://forum.qt.io/post/767830</link><guid isPermaLink="true">https://forum.qt.io/post/767830</guid><dc:creator><![CDATA[TikkiTxuck]]></dc:creator><pubDate>Mon, 07 Aug 2023 08:20:00 GMT</pubDate></item><item><title><![CDATA[Reply to How to propagate the wheelEvent to multiple widgets of my centralwidget on Mon, 07 Aug 2023 08:02:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/tikkitxuck">@<bdi>TikkiTxuck</bdi></a><br />
Make sure none of the child widgets propagate the event back to their parent?<br />
Start with a central widget which contains no child widgets.  Does that work or duplicate?  Then add a single child widget and check that.  Etc.</p>
]]></description><link>https://forum.qt.io/post/767828</link><guid isPermaLink="true">https://forum.qt.io/post/767828</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 07 Aug 2023 08:02:56 GMT</pubDate></item><item><title><![CDATA[Reply to How to propagate the wheelEvent to multiple widgets of my centralwidget on Mon, 07 Aug 2023 07:50:25 GMT]]></title><description><![CDATA[<p dir="auto">Ok! I make some progress base on your help and come to the following code:</p>
<pre><code>#include &lt;QtWidgets&gt;

class MyWidget : public QWidget {
public:
    MyWidget(QWidget *parent = nullptr) : QWidget(parent) {
    }

    void paintEvent(QPaintEvent *event) override {
        QPainter painter(this);
        painter.fillRect(rect(), Qt::white);
    }

protected:
    void wheelEvent(QWheelEvent *event) override {
        qDebug() &lt;&lt; "Wheel event on widget:" &lt;&lt; objectName();
        event-&gt;ignore();
    }
};

class EventFilter : public QObject {
    Q_OBJECT

public:
    EventFilter(QObject *parent = nullptr) : QObject(parent) {}

protected:
    bool eventFilter(QObject *obj, QEvent *event) override {
        if (event-&gt;type() == QEvent::Wheel) {
            QWheelEvent *wheelEvent = static_cast&lt;QWheelEvent*&gt;(event);

            // Propagate the wheel event to other widgets in the layout
            QList&lt;QWidget*&gt; widgets = obj-&gt;findChildren&lt;QWidget*&gt;();
            for (QWidget *widget : widgets) {
                if (widget != obj) {
                    QApplication::sendEvent(widget, new QWheelEvent(
                        wheelEvent-&gt;position(), wheelEvent-&gt;globalPosition(),
                        wheelEvent-&gt;pixelDelta(), wheelEvent-&gt;angleDelta(),
                        wheelEvent-&gt;buttons(), wheelEvent-&gt;modifiers(),
                        wheelEvent-&gt;phase(), wheelEvent-&gt;inverted()
                    ));
                }
            }

            event-&gt;accept();
            return true;
        }

        return QObject::eventFilter(obj, event);
    }
};

class MyMainWindow : public QMainWindow {
public:
    MyMainWindow(QWidget *parent = nullptr) : QMainWindow(parent) {
        setObjectName("MainWindow");

        centralWidget_ = new QWidget;
        centralWidget_-&gt;setObjectName("CentralWidget");
        layout_ = new QVBoxLayout(centralWidget_);

        for (int i = 0; i &lt; 4; ++i) {
            MyWidget *widget = new MyWidget(this);
            widget-&gt;setObjectName("Widget " + QString::number(i));
            layout_-&gt;addWidget(widget);
        }

        setCentralWidget(centralWidget_);
        installEventFilter(&amp;eventFilter_);
    }

private:
    QWidget *centralWidget_;
    QVBoxLayout *layout_;
    EventFilter eventFilter_;
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    MyMainWindow mainWindow;
    mainWindow.show();

    return app.exec();
}

#include "main.moc"
</code></pre>
<p dir="auto">As excepted, my event is propagated to the other widgets but the widget that originally received the event receive it twice. How to fix this ?</p>
]]></description><link>https://forum.qt.io/post/767826</link><guid isPermaLink="true">https://forum.qt.io/post/767826</guid><dc:creator><![CDATA[TikkiTxuck]]></dc:creator><pubDate>Mon, 07 Aug 2023 07:50:25 GMT</pubDate></item><item><title><![CDATA[Reply to How to propagate the wheelEvent to multiple widgets of my centralwidget on Fri, 04 Aug 2023 08:12:44 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/tikkitxuck">@<bdi>TikkiTxuck</bdi></a> said in <a href="/post/767514">How to propagate the wheelEvent to multiple widgets of my centralwidget</a>:</p>
<blockquote>
<p dir="auto">You mean calling installEventFilter on each widget</p>
</blockquote>
<p dir="auto">No, calling installEventFilter on top most widget (your main window for example)...</p>
]]></description><link>https://forum.qt.io/post/767518</link><guid isPermaLink="true">https://forum.qt.io/post/767518</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Fri, 04 Aug 2023 08:12:44 GMT</pubDate></item><item><title><![CDATA[Reply to How to propagate the wheelEvent to multiple widgets of my centralwidget on Fri, 04 Aug 2023 08:32:34 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/tikkitxuck">@<bdi>TikkiTxuck</bdi></a><br />
I knocked up the following simplest, standalone example from your code:</p>
<pre><code>#include &lt;QDebug&gt;
#include &lt;QLabel&gt;
#include &lt;QVBoxLayout&gt;
#include &lt;QWidget&gt;

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QVBoxLayout* vLayout = new QVBoxLayout();
    vLayout-&gt;setSpacing(0);

    QLabel* widget1 = new QLabel("widget1");
    QLabel* widget2 = new QLabel("widget2");
    vLayout-&gt;addWidget(widget1);
    vLayout-&gt;addWidget(widget2);

    QWidget* centralWidget = new QWidget();
    centralWidget-&gt;setLayout(vLayout);

    setCentralWidget(centralWidget);

    installEventFilter(this);
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (event-&gt;type() == QEvent::Wheel)
    {
        qDebug() &lt;&lt; "Wheelevent";
        return true;
    }

    return QMainWindow::eventFilter(obj, event);
}
</code></pre>
<p dir="auto">I run Qt 5.15 under Linux (Xorg desktop).  I do not have Qt6.  This code debugs out "Wheelevent" when I roll the wheel <em>anywhere</em> over the main window, whether inside the <code>QLabel</code>s or not.</p>
<p dir="auto">So either it's different at Qt6 (doubt it), I have misunderstood and this code does not represent your situation (I think it does) or where you say</p>
<blockquote>
<p dir="auto">MyWidget which has the wheelEvent function overrided</p>
</blockquote>
<p dir="auto">maybe if your override "eats" the wheel event (i.e. returns <code>true</code>) then that prevents the propagation you are wanting?<br />
<strong>UPDATE</strong><br />
I tried subclassing the <code>QLabel</code>s and overriding the <code>eventFilter()</code>.  I still always received the <code>MainWindow</code>s wheel event, after the label's wheel event, regardless of whether the latter's <code>eventFilter()</code> returned true or false.  So it seems you should too?</p>
]]></description><link>https://forum.qt.io/post/767517</link><guid isPermaLink="true">https://forum.qt.io/post/767517</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 04 Aug 2023 08:32:34 GMT</pubDate></item><item><title><![CDATA[Reply to How to propagate the wheelEvent to multiple widgets of my centralwidget on Fri, 04 Aug 2023 07:48:33 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> Thanks for the welcoming !</p>
<p dir="auto">You mean calling <code>installEventFilter</code> on each widget which must receive the propagated event ? Can you provide some code example please ?</p>
]]></description><link>https://forum.qt.io/post/767514</link><guid isPermaLink="true">https://forum.qt.io/post/767514</guid><dc:creator><![CDATA[TikkiTxuck]]></dc:creator><pubDate>Fri, 04 Aug 2023 07:48:33 GMT</pubDate></item><item><title><![CDATA[Reply to How to propagate the wheelEvent to multiple widgets of my centralwidget on Fri, 04 Aug 2023 07:41:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/tikkitxuck">@<bdi>TikkiTxuck</bdi></a> Shouldn't you install it in the top widget where you want to catch the events?</p>
]]></description><link>https://forum.qt.io/post/767512</link><guid isPermaLink="true">https://forum.qt.io/post/767512</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Fri, 04 Aug 2023 07:41:17 GMT</pubDate></item><item><title><![CDATA[Reply to How to propagate the wheelEvent to multiple widgets of my centralwidget on Fri, 04 Aug 2023 07:31:20 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> I tried to install it on <code>qApp</code>, on <code>QCoreApplication::instance()</code> and on <code>QApplication::instance()</code>, but none of them was working. Certainly because I miss something, but I don't know what :/</p>
]]></description><link>https://forum.qt.io/post/767510</link><guid isPermaLink="true">https://forum.qt.io/post/767510</guid><dc:creator><![CDATA[TikkiTxuck]]></dc:creator><pubDate>Fri, 04 Aug 2023 07:31:20 GMT</pubDate></item><item><title><![CDATA[Reply to How to propagate the wheelEvent to multiple widgets of my centralwidget on Thu, 03 Aug 2023 14:22:21 GMT]]></title><description><![CDATA[<p dir="auto">Hi and welcome to devnet,</p>
<p dir="auto">Might be a silly question but did you install the filter on your widgets ?</p>
]]></description><link>https://forum.qt.io/post/767429</link><guid isPermaLink="true">https://forum.qt.io/post/767429</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 03 Aug 2023 14:22:21 GMT</pubDate></item><item><title><![CDATA[Reply to How to propagate the wheelEvent to multiple widgets of my centralwidget on Thu, 03 Aug 2023 14:22:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/tikkitxuck">@<bdi>TikkiTxuck</bdi></a><br />
While you wait for someone who knows better than I.  I believe wheel events go to the focussed window first, and if that consumes them (yours does) they do not get propagated higher up the tree to your man window.</p>
<p dir="auto">You may have to install the <code>eventFilter()</code> on the <em>application</em> rather than the main window?</p>
]]></description><link>https://forum.qt.io/post/767428</link><guid isPermaLink="true">https://forum.qt.io/post/767428</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 03 Aug 2023 14:22:16 GMT</pubDate></item></channel></rss>