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)
QVariant 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 XML 有许多标准光标形状,但您也可以根据QBitmap 、遮罩和热点制作自定义光标形状。

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

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

如果想使用自己的位图创建游标,可以使用 QCursor 构造函数(该函数以位图和掩码为参数)或以像素图为参数的构造函数。

要设置或获取鼠标光标的位置,请使用静态方法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 表示透明。
  • 在 Windows 下,B=1 和 M=0 会产生一个 XOR 结果,而在所有其他平台上则会产生未定义的结果。

在位图中,使用全局 Qt XML 颜色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() 将其转换为 widget 坐标。

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

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

另请参阅 setPos(),QWidget::mapFromGlobal(),QWidget::mapToGlobal() 和QGuiApplication::primaryScreen().

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

返回screen 光标(热点)在全屏坐标中的位置。

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

另请参阅 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 互换。该操作速度非常快,而且从未出现过故障。

QVariant QCursor::operator QVariant() const

QVariant 的形式返回游标。

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

Move-assignsother 到此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)

相等运算符。如果lhsrhsshape() 相同,则返回true ;如果bitmap cursors 相同,则返回hotSpot() 和pixmap() 或bitmap() 和mask() 相同。

注意: 比较位图游标时,此函数只比较位图的cache keys ,而不是每个像素。

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

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

stream 读取cursor

另请参阅 序列化 Qt 数据类型

© 2025 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.