2

当鼠标不在QTextEdit上时转动鼠标滚轮时,滚动条不会移动,但我仍然想通过鼠标滚轮移动滚动条,那么我该如何实现这个功能呢?我知道有些软件像 Microsoft Word 有这个功能。

我实现了这个功能,如下所示,但是当您通过鼠标滚轮将滚动条移动到顶部或底部时,会出现错误:调用 Python 对象时超出最大递归深度。任何人都可以帮忙吗?我的代码在这里http://codepad.org/1rq06qTk

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *


class BoxLayout(QWidget):
    def __init__(self, parent=None):
        super(BoxLayout, self).__init__(parent)
        self.resize(100, 300)

        ok = QPushButton("OK")
        cancel = QPushButton("Cancel")
        self.textEdit = QTextEdit("This function returns true if the contents "
                                  "of the MIME data object, specified by source, "
                                  "can be decoded and inserted into the document. "
                                  "It is called for example when during a drag "
                                  "operation the mouse enters this widget and it "
                                  "is necessary to determine whether it is possible "
                                  "to accept the drag and drop operation.")

        vbox = QVBoxLayout()
        vbox.addWidget(self.textEdit)
        vbox.addWidget(ok)
        vbox.addWidget(cancel)
        self.setLayout(vbox)

#       self.textEdit.installEventFilter(self)


#    def eventFilter(self, obj, event):
#        if obj == self.textEdit:
#            if event.type() == QEvent.Wheel:
#                self.textEdit.wheelEvent(event)
#                return True
#            else:
#                return False
#        else:
#            return QMainWindow.eventFilter(self, obj, event)


    def wheelEvent(self, event):
        self.textEdit.wheelEvent(event)


app = QApplication(sys.argv)
qb = BoxLayout()
qb.show()
sys.exit(app.exec_())
4

2 回答 2

1

您可以通过在将鼠标滚轮事件转发到 QTextEdit 的任何小部件上安装事件过滤器来做到这一点:

不幸的是,我只能给你 C++ 代码,但它应该给你一个大致的想法。另外,我使用的是 Qt 4.6.1。我在 Qt 4.8 及更高版本中检测到一个新的“滚动”事件类型,您可能需要在使用它时更改代码。


[编辑:]

对于 TextEdit 滚动,由于某种原因,事件处理与标准方式不同:将 Wheel Event 发送到 Text Edit 不会对其进行处理。相反,不幸的是wheelEvent,某种私有事件过滤器调用受到保护。QAbstractScrollArea::viewportEvent

长话短说,我们可以通过继承 QTextEdit 来伪造 Wheel Events:

#include "MyTextEdit.h"

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

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

当使用它而不是默认的 QTextEdits 时,您可以像这样创建一个事件转发器:


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

然后你可以像这样安装一个事件过滤器:

MainWindow.cpp (或任何更合适的地方):

_wheelEventForwarder = new WheelEventForwarder( ui->textEdit );

ui->centralwidget->installEventFilter( _wheelEventForwarder );

有关这方面的更多信息,请参阅QObject::installEventFilter的文档。

于 2013-07-19T07:05:21.297 回答
0

您也可以采用这种方法:

调用 QTextEdit.scroll(int,int) 函数在需要时滚动 qtextedit。意味着您可以从小部件的 mouseScrollEvent 调用此函数

于 2013-07-20T09:37:57.630 回答