how to redirect Wheel events of QWidget to QTextEdit

坚强是说给别人听的谎言 提交于 2019-12-06 14:05:18

You could do this by installing an event filter on any widget which shall forward the mouse wheel event to the QTextEdit:

I can unfortunately only give you C++ code but it should give you a general idea. Also, I'm using Qt 4.6.1. I've detected a new "Scroll" Event type in Qt 4.8 and later, you might have to change the code when using that.


[Edit:]

For TextEdit scrolling, event processing differs from the standard way for some reason: Sending a Wheel Event to the Text Edit does not process it. Instead, some kind of private event filter calls wheelEvent through QAbstractScrollArea::viewportEvent which is unfortunately protected.

Long story short, we can fake Wheel Events by subclassing QTextEdit:

#include "MyTextEdit.h"

MyTextEdit::MyTextEdit( QWidget* parent ) :
    QTextEdit( parent )
{
}

void MyTextEdit::forwardViewportEvent( QEvent* event )
{
    viewportEvent( event );
}

When using this instead of the default QTextEdits, you can create an Event Forwarder like this:


#ifndef WHEELEVENTFORWARDER_H
#define WHEELEVENTFORWARDER_H

#include <QtCore/QObject>

class MyTextEdit;

class WheelEventForwarder : public QObject
{
    Q_OBJECT
public:
    explicit WheelEventForwarder( MyTextEdit* target );
    ~WheelEventForwarder();

    bool eventFilter( QObject* obj, QEvent* event );

private:
    MyTextEdit* _target;
};

#endif // WHEELEVENTFORWARDER_H

WheelEventForwarder.cpp

#include "WheelEventForwarder.h"
#include "MyTextEdit.h"
#include <QtCore/QEvent>
#include <QtGui/QApplication>

WheelEventForwarder::WheelEventForwarder( MyTextEdit* target ) :
    QObject(),
    _target( target )
{
}

WheelEventForwarder::~WheelEventForwarder()
{
    _target = NULL;
}

bool WheelEventForwarder::eventFilter( QObject* obj, QEvent* event )
{
    Q_UNUSED( obj );

    static bool recursionProtection = false;

    if( recursionProtection ) return false;

    if( !_target ) return false;

    if( event->type() == QEvent::Wheel )
    {
        recursionProtection = true;
        _target->forwardViewportEvent( event );
        recursionProtection = false;
    }

    // do not filter the event
    return false;
}

Then you can install an event filter like this:

MainWindow.cpp (or any better fitting place):

_wheelEventForwarder = new WheelEventForwarder( ui->textEdit );

ui->centralwidget->installEventFilter( _wheelEventForwarder );

See the documentation for QObject::installEventFilter for more information on this.

You can also go with this approach:

Call QTextEdit.scroll(int,int) function to scroll qtextedit whenever required. Mean you can cal this function from the mouseScrollEvent of your widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!