QWheelEvent 类

QWheelEvent 类包含滚轮事件的描述参数。 更多...

头: #include <QWheelEvent>
qmake: QT += gui
继承: QInputEvent

公共函数

QWheelEvent (QPointF pos , QPointF globalPos , QPoint pixelDelta , QPoint angleDelta , Qt::MouseButtons buttons , Qt::KeyboardModifiers modifiers , Qt::ScrollPhase phase , bool inverted , Qt::MouseEventSource source = Qt::MouseEventNotSynthesized)
QPoint angleDelta () const
Qt::MouseButtons buttons () const
QPointF globalPosition () const
bool inverted () const
Qt::ScrollPhase phase () const
QPoint pixelDelta () const
QPointF position () const
Qt::MouseEventSource source () const

详细描述

Wheel events are sent to the widget under the mouse cursor, but if that widget does not handle the event they are sent to the focus widget. Wheel events are generated for both mouse wheels and trackpad scroll gestures. There are two ways to read the wheel event delta: angleDelta () returns the deltas in wheel degrees. These values are always provided. pixelDelta () returns the deltas in screen pixels, and is available on platforms that have high-resolution trackpads, such as macOS. If that is the case, source () 会返回 Qt::MouseEventSynthesizedBySystem .

The functions pos() and globalPos() return the mouse cursor's location at the time of the event.

A wheel event contains a special accept flag that indicates whether the receiver wants the event. You should call ignore () if you do not handle the wheel event; this ensures that it will be sent to the parent widget.

QWidget::setEnabled () 函数可用于启用 (或禁用) 小部件的鼠标事件和键盘事件。

事件处理程序 QWidget::wheelEvent () 接收滚轮事件。

另请参阅 QMouseEvent and QWidget::grabMouse ().

成员函数文档编制

QWheelEvent:: QWheelEvent ( QPointF pos , QPointF globalPos , QPoint pixelDelta , QPoint angleDelta , Qt::MouseButtons buttons , Qt::KeyboardModifiers modifiers , Qt::ScrollPhase phase , bool inverted , Qt::MouseEventSource source = Qt::MouseEventNotSynthesized)

构造滚轮事件对象。

pos provides the location of the mouse cursor within the window. The position in global coordinates is specified by globalPos .

pixelDelta contains the scrolling distance in pixels on screen, while angleDelta contains the wheel rotation angle. pixelDelta is optional and can be null.

The mouse and keyboard states at the time of the event are specified by buttons and modifiers .

The scrolling phase of the event is specified by phase .

If the wheel event comes from a physical mouse wheel, source 被设为 Qt::MouseEventNotSynthesized . If it comes from a gesture detected by the operating system, or from a non-mouse hardware device, such that pixelDelta is directly related to finger movement, source 被设为 Qt::MouseEventSynthesizedBySystem . If it comes from Qt, source would be set to Qt::MouseEventSynthesizedByQt .

If the system is configured to invert the delta values delivered with the event (such as natural scrolling of the touchpad on macOS), inverted 应该为 true 。否则, inverted is false

该函数在 Qt 5.12 引入。

另请参阅 position (), globalPosition (), angleDelta (), pixelDelta (), phase (), inverted (),和 source ().

QPoint QWheelEvent:: angleDelta () const

Returns the relative amount that the wheel was rotated, in eighths of a degree. A positive value indicates that the wheel was rotated forwards away from the user; a negative value indicates that the wheel was rotated backwards toward the user. angleDelta().y() provides the angle through which the common vertical mouse wheel was rotated since the previous event. angleDelta().x() provides the angle through which the horizontal mouse wheel was rotated, if the mouse has a horizontal wheel; otherwise it stays at zero. Some mice allow the user to tilt the wheel to perform horizontal scrolling, and some touchpads support a horizontal scrolling gesture; that will also appear in angleDelta().x() .

Most mouse types work in steps of 15 degrees, in which case the delta value is a multiple of 120; i.e., 120 units * 1/8 = 15 degrees.

However, some mice have finer-resolution wheels and send delta values that are less than 120 units (less than 15 degrees). To support this possibility, you can either cumulatively add the delta values from events until the value of 120 is reached, then scroll the widget, or you can partially scroll the widget in response to each wheel event. But to provide a more native feel, you should prefer pixelDelta () on platforms where it's available.

范例:

void MyWidget::wheelEvent(QWheelEvent *event)
{
    QPoint numPixels = event->pixelDelta();
    QPoint numDegrees = event->angleDelta() / 8;
    if (!numPixels.isNull()) {
        scrollWithPixels(numPixels);
    } else if (!numDegrees.isNull()) {
        QPoint numSteps = numDegrees / 15;
        scrollWithDegrees(numSteps);
    }
    event->accept();
}
					

注意: 当平台支持卷动 phases ,增量可能为 null 当:

  • scrolling is about to begin, but the distance did not yet change ( Qt::ScrollBegin ),
  • or scrolling has ended and the distance did not change anymore ( Qt::ScrollEnd ).

另请参阅 pixelDelta ().

Qt::MouseButtons QWheelEvent:: buttons () const

Returns the mouse state when the event occurred.

QPointF QWheelEvent:: globalPosition () const

Returns the global position of the mouse pointer 在事件发生时 。这对异步窗口系统很重要,譬如 X11;每当围绕鼠标事件响应移动 Widget 时,globalposition() 可能非常不同于当前光标位置返回通过 QCursor::pos ().

另请参阅 position ().

bool QWheelEvent:: inverted () const

Returns whether the delta values delivered with the event are inverted.

Normally, a vertical wheel will produce a QWheelEvent with positive delta values if the top of the wheel is rotating away from the hand operating it. Similarly, a horizontal wheel movement will produce a QWheelEvent with positive delta values if the top of the wheel is moved to the left.

However, on some platforms this is configurable, so that the same operations described above will produce negative delta values (but with the same magnitude). With the inverted property a wheel event consumer can choose to always follow the direction of the wheel, regardless of the system settings, but only for specific widgets. (One such use case could be that the user is rotating the wheel in the same direction as a visual Tumbler rotates. Another usecase is to make a slider handle follow the direction of movement of fingers on a touchpad regardless of system configuration.)

注意: Many platforms provide no such information. On such platforms inverted always returns false.

该函数在 Qt 5.7 引入。

Qt::ScrollPhase QWheelEvent:: phase () const

返回此滚轮事件的卷动阶段。

注意: Qt::ScrollBegin and Qt::ScrollEnd 阶段目前仅 macOS 支持。

该函数在 Qt 5.2 引入。

QPoint QWheelEvent:: pixelDelta () const

Returns the scrolling distance in pixels on screen. This value is provided on platforms that support high-resolution pixel-based delta values, such as macOS. The value should be used directly to scroll content on screen.

范例:

void MyWidget::wheelEvent(QWheelEvent *event)
{
    QPoint numPixels = event->pixelDelta();
    QPoint numDegrees = event->angleDelta() / 8;
    if (!numPixels.isNull()) {
        scrollWithPixels(numPixels);
    } else if (!numDegrees.isNull()) {
        QPoint numSteps = numDegrees / 15;
        scrollWithDegrees(numSteps);
    }
    event->accept();
}
					

注意: 当平台支持卷动 phases ,增量可能为 null 当:

  • scrolling is about to begin, but the distance did not yet change ( Qt::ScrollBegin ),
  • or scrolling has ended and the distance did not change anymore ( Qt::ScrollEnd ).

注意: On X11 this value is driver specific and unreliable, use angleDelta () instead

QPointF QWheelEvent:: position () const

Returns the position of the mouse cursor relative to the widget that received the event.

若围绕鼠标事件响应移动 Widget,使用 globalPosition () 而不是此函数。

该函数在 Qt 5.14 引入。

另请参阅 globalPosition ().

Qt::MouseEventSource QWheelEvent:: source () const

Returns information about the wheel event source.

The source can be used to distinguish between events that come from a mouse with a physical wheel and events that are generated by some other means, such as a flick gesture on a touchpad.

注意: Many platforms provide no such information. On such platforms Qt::MouseEventNotSynthesized is returned always.

该函数在 Qt 5.5 引入。

另请参阅 Qt::MouseEventSource .