QCursor 类

QCursor class provides a mouse cursor with an arbitrary shape. 更多...

头: #include <QCursor>
qmake: QT += gui

公共函数

QCursor ()
QCursor (Qt::CursorShape shape )
QCursor (const QBitmap & bitmap , const QBitmap & mask , int hotX = -1, int hotY = -1)
QCursor (const QPixmap & pixmap , int hotX = -1, int hotY = -1)
QCursor (const QCursor & c )
QCursor (QCursor && other )
~QCursor ()
const QBitmap * bitmap () const
QPoint hotSpot () const
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= (const QCursor & c )
QCursor & operator= (QCursor && other )

静态公共成员

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 )
QDataStream & operator<< (QDataStream & stream , const QCursor & cursor )
QDataStream & operator>> (QDataStream & stream , QCursor & cursor )

详细描述

QCursor class provides a mouse cursor with an arbitrary shape.

此类主要用于创建关联特定 Widget 的鼠标光标,及获取和设置鼠标光标的位置。

Qt 有许多标准光标形状,但也可以制作自定义光标形状基于 QBitmap 、遮罩及热点。

Widget 要关联光标,使用 QWidget::setCursor ()。要关联光标与所有 Widget (通常为短时间周期),使用 QGuiApplication::setOverrideCursor ().

要设置光标形状使用 QCursor::setShape () or use the QCursor constructor which takes the shape as argument, or you can use one of the predefined cursors defined in the Qt::CursorShape 枚举。

If you want to create a cursor with your own bitmap, either use the QCursor constructor which takes a bitmap and a mask or the constructor which takes a pixmap as arguments.

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

注意: It is possible to create a QCursor before QGuiApplication , but it is not useful except as a place-holder for a real QCursor created after QGuiApplication . Attempting to use a QCursor that was created before QGuiApplication 将导致崩溃。

X11 用户注意事项

在 X11,Qt 支持 Xcursor 库,允许全彩图标主题。下表展示的光标名称被用于每 Qt::CursorShape 值。若使用以下展示的名称找不到光标,则使用标准 X11 光标代替。注意:X11 没有提供适当光标为所有可能的 Qt::CursorShape 值。某些游标可能获取自 Xcursor 主题,而其它的使用内部位图光标。

形状 Qt::CursorShape 光标名称 形状 Qt::CursorShape 光标名称
Qt::ArrowCursor left_ptr Qt::SizeVerCursor size_ver
Qt::UpArrowCursor up_arrow Qt::SizeHorCursor size_hor
Qt::CrossCursor cross Qt::SizeBDiagCursor size_bdiag
Qt::IBeamCursor ibeam Qt::SizeFDiagCursor size_fdiag
Qt::WaitCursor wait Qt::SizeAllCursor size_all
Qt::BusyCursor left_ptr_watch Qt::SplitVCursor split_v
Qt::ForbiddenCursor forbidden Qt::SplitHCursor split_h
Qt::PointingHandCursor pointing_hand Qt::OpenHandCursor openhand
Qt::WhatsThisCursor whats_this Qt::ClosedHandCursor closedhand
Qt::DragMoveCursor dnd-move or move Qt::DragCopyCursor dnd-copy or copy
Qt::DragLinkCursor dnd-link or link

另请参阅 QWidget and GUI 设计手册:光标 .

成员函数文档编制

QCursor:: QCursor ()

采用默认箭头形状构造光标。

QCursor:: QCursor ( Qt::CursorShape shape )

构造光标采用指定 shape .

Qt::CursorShape 为形状列表。

另请参阅 setShape ().

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

构造自定义位图光标。

bitmap and mask 构成位图。 hotX and 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 QPixmap & pixmap , int hotX = -1, int hotY = -1)

构造自定义像素图光标。

pixmap 是图像。它通常给出遮罩 (设置使用 QPixmap::setMask ()). hotX and 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 QCursor & c )

构造拷贝为光标 c .

QCursor:: QCursor ( QCursor && other )

移动构造光标来自 other 。在移动之后,唯一有效操作对于 other 被销毁并 (移动和复制) 赋值。在移动实例上调用任何其他成员函数的效果,是不确定的。

该函数在 Qt 5.5 引入。

QCursor:: ~QCursor ()

销毁光标。

const QBitmap *QCursor:: bitmap () const

Returns the cursor bitmap, or 0 if it is one of the standard cursors.

QPoint QCursor:: hotSpot () const

返回光标热点,或 (0, 0) 若它是标准光标之一。

const QBitmap *QCursor:: mask () const

Returns the cursor bitmap mask, or 0 if it is one of the standard cursors.

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 () 以将 Widget 坐标翻译成全局屏幕坐标。

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

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

移动光标 (热点) 为 screen 到全局屏幕位置 ( x , y ).

可以调用 QWidget::mapToGlobal () 以将 Widget 坐标翻译成全局屏幕坐标。

注意: 调用此函数将导致透过窗口系统,改变光标位置。窗口系统通常会通过向应用程序窗口发送鼠标事件,来响应。这意味着在单元测试和凭借 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

返回光标形状标识符。返回值是某一 Qt::CursorShape 枚举值 (铸造成 int)。

另请参阅 setShape ().

void QCursor:: swap ( QCursor & other )

QCursor:: operator QVariant () const

返回光标作为 QVariant .

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

赋值 c 给此光标并返回此光标的引用。

QCursor &QCursor:: operator= ( QCursor && other )

移动赋值 other 到此 QCursor 实例。

该函数在 Qt 5.2 引入。

相关非成员

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

写入 cursor stream .

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

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

读取 cursor stream .

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