本页内容

QCursor Class

QCursor 类提供了一个具有任意形状的鼠标光标。更多内容...

头文件: #include <QCursor>
CMake: find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
qmake: QT += gui

公共函数

QCursor()
QCursor(Qt::CursorShape shape)
QCursor(const QPixmap &pixmap, int hotX = -1, int hotY = -1)
QCursor(const QBitmap &bitmap, const QBitmap &mask, int hotX = -1, int hotY = -1)
QCursor(const QCursor &c)
QCursor(QCursor &&other)
~QCursor()
QBitmap bitmap() const
QPoint hotSpot() const
QBitmap mask() const
QPixmap pixmap() const
void setShape(Qt::CursorShape shape)
Qt::CursorShape shape() const
void swap(QCursor &other)
operator QVariant() const
QCursor &operator=(QCursor &&other)
QCursor &operator=(const QCursor &c)

静态公共成员

QPoint pos()
QPoint pos(const QScreen *screen)
void setPos(int x, int y)
void setPos(QScreen *screen, int x, int y)
void setPos(const QPoint &p)
void setPos(QScreen *screen, const QPoint &p)
bool operator!=(const QCursor &lhs, const QCursor &rhs)
QDataStream &operator<<(QDataStream &stream, const QCursor &cursor)
bool operator==(const QCursor &lhs, const QCursor &rhs)
QDataStream &operator>>(QDataStream &stream, QCursor &cursor)

详细描述

该类主要用于创建与特定小部件关联的鼠标光标,以及获取和设置鼠标光标的位置。

Qt 提供了一些标准的光标形状,但您也可以基于QBitmap 、蒙版和热点来制作自定义光标形状。

要将光标与小部件关联,请使用 `QWidget::setCursor()`。要将光标与所有小部件关联(通常仅在短时间内),请使用 `QGuiApplication::setOverrideCursor()`。

要设置光标形状,请使用 `QCursor::setShape()` 方法,或使用将形状作为参数的 `QCursor` 构造函数,或者您可以使用 `Qt::CursorShape ` 枚举中定义的预定义光标之一。

如果要使用自己的位图创建光标,可以使用接受位图和掩码作为参数的 QCursor 构造函数,或者使用接受 pixmap 作为参数的构造函数。

要设置或获取鼠标光标的位置,请使用静态方法QCursor::pos() 和QCursor::setPos()。

注意:虽然可以在调用QGuiApplication 之前创建一个 QCursor,但这除了作为QGuiApplication 之后创建的真实 QCursor 的占位符外,并无实际用途。尝试使用在QGuiApplication 之前创建的 QCursor 将导致程序崩溃。

致 X11 用户的说明

在 X11 环境下,Qt 支持Xcursor库,该库允许使用全彩图标主题。下表列出了每个Qt::CursorShape 对应的游标名称。如果无法通过下表所示的名称找到相应的游标,则会改用标准的 X11 游标。 注意:X11 并非为所有可能的Qt::CursorShape 值都提供了相应的光标。部分光标可能取自 Xcursor 主题,而其他光标则会使用内部位图光标。

另请参阅 QWidget 。

成员函数文档

QCursor::QCursor()

创建一个具有默认箭头形状的光标。

QCursor::QCursor(Qt::CursorShape shape)

根据指定的shape 创建一个游标。

有关形状的列表,请参阅Qt::CursorShape 。

另请参阅 setShape()。

[explicit] QCursor::QCursor(const QPixmap &pixmap, int hotX = -1, int hotY = -1)

构建一个自定义位图光标。

pixmap 是图像。通常会为其指定一个蒙版(使用QPixmap::setMask()设置)。hotX 和hotY 定义光标的热区。

如果hotX 为负值,则将其设置为pixmap().width()/2 。如果hotY 为负值,则将其设置为pixmap().height()/2 。

有效的光标尺寸取决于显示硬件(或底层窗口系统)。我们建议使用 32 x 32 的光标,因为该尺寸在所有平台上均受支持。某些平台还支持 16 x 16、48 x 48 和 64 x 64 的光标。

另请参阅 QPixmap::QPixmap() 和QPixmap::setMask()。

QCursor::QCursor(const QBitmap &bitmap, const QBitmap &mask, int hotX = -1, int hotY = -1)

构建一个自定义位图光标。

bitmap mask 共同构成位图。 和 定义光标的热区。hotX hotY

如果hotX 为负数,则将其设置为bitmap().width()/2 。如果hotY 为负数,则将其设置为bitmap().height()/2 。

光标的bitmap (B)和mask (M)位按以下方式组合:

  • 当 B=1 且 M=1 时,显示黑色。
  • B=0 且 M=1 时显示为白色。
  • 当 B=0 且 M=0 时,显示为透明。
  • 当 B=1 且 M=0 时,在 Windows 系统下结果为异或(XOR)运算结果,而在其他所有平台上结果未定义。

使用全局 Qt 颜色Qt::color0 在位图中绘制 0 像素,使用Qt::color1 绘制 1 像素。

有效的光标大小取决于显示硬件(或底层窗口系统)。我们建议使用 32 x 32 的光标,因为该尺寸在所有平台上均受支持。某些平台还支持 16 x 16、48 x 48 和 64 x 64 的光标。

另请参阅 QBitmap::QBitmap() 和QBitmap::setMask()。

QCursor::QCursor(const QCursor &c)

创建光标c 的副本。

[noexcept] QCursor::QCursor(QCursor &&other)

从other 移动并构造一个游标。从该对象移动后,other 上唯一有效的操作是销毁以及(移动和复制)赋值。对已移动的实例调用任何其他成员函数,其效果未定义。

[noexcept] QCursor::~QCursor()

删除光标。

QBitmap QCursor::bitmap() const

返回光标位图;如果是标准光标之一,则返回空位图。

QPoint QCursor::hotSpot() const

返回光标的热区位置;如果该光标是标准光标之一,则返回 (0, 0)。

QBitmap QCursor::mask() const

返回光标位图掩码;如果该光标属于标准光标之一,则返回空位图。

QPixmap QCursor::pixmap() const

返回光标位图。只有当光标是位图光标时,此操作才有效。

[static] QPoint QCursor::pos()

返回主屏幕光标(热区)在全局屏幕坐标系中的位置。

您可以调用QWidget::mapFromGlobal()将其转换为控件坐标系。

注意:该 位置是从窗口系统中查询得到的。如果鼠标事件是通过其他方式生成的(例如,在单元测试中通过 QWindowSystemInterface 生成),则这些模拟的鼠标移动不会反映在返回值中。

注意:在 没有窗口系统或无法使用光标的平台上, 返回的位置基于通过 QWindowSystemInterface 生成的鼠标移动事件。

另请参阅 setPos()、QWidget::mapFromGlobal()、QWidget::mapToGlobal() 以及QGuiApplication::primaryScreen()。

[static] QPoint QCursor::pos(const QScreen *screen)

返回screen 光标(热区)在全局屏幕坐标系中的位置。

您可以调用 `QWidget::mapFromGlobal()` 将其转换为控件坐标系。

另请参阅 setPos()、QWidget::mapFromGlobal() 和QWidget::mapToGlobal()。

[static] void QCursor::setPos(int x, int y)

将主屏幕的光标(热区)移动到全局屏幕位置(x ,y )。

您可以调用QWidget::mapToGlobal() 函数,将控件坐标转换为全局屏幕坐标。

另请参阅 pos()、QWidget::mapFromGlobal()、QWidget::mapToGlobal() 和QGuiApplication::primaryScreen()。

[static] void QCursor::setPos(QScreen *screen, int x, int y)

将screen 的游标(热区)移动到全局屏幕位置(x ,y )。

您可以调用 `QWidget::mapToGlobal()` 函数,将控件坐标转换为全局屏幕坐标。

注意:调用 此函数会导致通过窗口系统改变光标位置。窗口系统通常会通过向应用程序窗口发送鼠标事件来响应。 这意味着,在单元测试中以及通过 QWindowSystemInterface 注入伪鼠标事件的任何情况下,都应避免使用此函数,因为窗口系统的鼠标状态(例如与按钮相关的状态)可能与应用程序生成的事件中的状态不匹配。

注意:在 没有窗口系统或无法使用光标的平台上, 此函数可能不会执行任何操作。

另请参阅 pos()、QWidget::mapFromGlobal() 和QWidget::mapToGlobal()。

[static] void QCursor::setPos(const QPoint &p)

将光标(热点)移动到全局屏幕坐标p 处。

这是一个重载函数。

[static] void QCursor::setPos(QScreen *screen, const QPoint &p)

将光标(热点)移动到screen 在坐标p 处的全局屏幕位置。

这是一个重载函数。

void QCursor::setShape(Qt::CursorShape shape)

将光标设置为由shape 指定的形状。

有关光标形状的列表,请参阅Qt::CursorShape 。

另请参阅 shape()。

Qt::CursorShape QCursor::shape() const

返回光标形状标识符。

另请参阅 ` setShape()`。

[noexcept] void QCursor::swap(QCursor &other)

将此光标与other 互换。此操作速度极快,且绝不会失败。

QCursor::operator QVariant() const

将光标作为QVariant 返回。

[noexcept] QCursor &QCursor::operator=(QCursor &&other)

将other 通过Move操作赋值给此QCursor 实例。

QCursor &QCursor::operator=(const QCursor &c)

将c 分配给此游标,并返回对此游标的引用。

相关的非成员

[noexcept] bool operator!=(const QCursor &lhs, const QCursor &rhs)

不等号运算符。返回等同于 !(lhs ==rhs) 的结果。

另请参阅 operator==(const QCursor &lhs, const QCursor &rhs)。

QDataStream &operator<<(QDataStream &stream, const QCursor &cursor)

将cursor 写入stream 。

另请参阅 《Qt 数据类型的序列化》。

[noexcept] bool operator==(const QCursor &lhs, const QCursor &rhs)

相等运算符。如果lhs 和rhs 具有相同的shape(),且在bitmap cursors 的情况下,具有相同的hotSpot(),并且要么具有相同的pixmap(),要么具有相同的bitmap()和mask(),则返回true 。

注意:在 比较位图光标时 ,此函数仅比较位图的cache keys ,而非每个像素。

另请参阅 operator!=(const QCursor &lhs, const QCursor &rhs)。

QDataStream &operator>>(QDataStream &stream, QCursor &cursor)

从stream 中读取cursor 。

另请参阅 《Qt 数据类型的序列化》。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.