QCursor 类

The QCursor 类提供具有任意形状的鼠标光标。 更多...

头: #include <QCursor>

公共函数

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 (HCURSOR cursor )
QCursor (Qt::HANDLE handle )
~QCursor ()
const QBitmap * bitmap () const
HCURSOR_or_HANDLE handle () const
QPoint hotSpot () const
const QBitmap * mask () const
QPixmap pixmap () const
void setShape (Qt::CursorShape shape )
Qt::CursorShape shape () const
operator QVariant () const
QCursor & operator= (const QCursor & c )
QCursor & operator= (QCursor && other )

静态公共成员

QPoint pos ()
void setPos (int x , int y )
void setPos (const QPoint & p )
QDataStream & operator<< (QDataStream & stream , const QCursor & cursor )
QDataStream & operator>> (QDataStream & stream , QCursor & cursor )

详细描述

The QCursor 类提供具有任意形状的鼠标光标。

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

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

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

要设置光标形状使用 QCursor::setShape () 或使用 QCursor 构造函数接受形状作为自变量,或者可以使用某一预定义光标的定义在 Qt::CursorShape 枚举。

若想要采用自己的位图创建光标,要么使用 QCursor 构造函数接受位图和遮罩作为自变量,或构造函数接受像素图作为自变量。

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

注意: 它是可能的创建 QCursor before QApplication ,但它没什么用,除了作为占位符对于真实 QCursor 的创建后于 QApplication 。试图使用 QCursor 创建的先于 QApplication 将导致崩溃。

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 光标。

注意: On Windows CE, the cursor size is fixed. If the pixmap is bigger than the system size, it will be scaled.

另请参阅 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 光标。

注意: On Windows CE, the cursor size is fixed. If the pixmap is bigger than the system size, it will be scaled.

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

QCursor:: QCursor (const QCursor & c )

构造拷贝为光标 c .

QCursor:: QCursor ( HCURSOR cursor )

Constructs a Qt cursor from the given Windows cursor .

警告: This function is only available on Windows.

另请参阅 handle ().

QCursor:: QCursor ( Qt::HANDLE handle )

Constructs a Qt cursor from the given handle .

警告: This function is only available on X11.

另请参阅 handle ().

QCursor:: ~QCursor ()

销毁光标。

const QBitmap * QCursor:: bitmap () const

返回光标位图,或 0 若它是标准光标之一。

HCURSOR_or_HANDLE QCursor:: handle () const

Returns a platform-specific cursor handle. The HCURSOR_or_HANDLE type is HCURSOR 在 Windows 和 Qt::HANDLE on X11 and Mac OS X. On Qt for Embedded Linux it is an integer.

警告: Using the value returned by this function is not portable.

QPoint QCursor:: hotSpot () const

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

const QBitmap * QCursor:: mask () const

返回光标位图遮罩,或 0 若它是标准光标之一。

QPixmap QCursor:: pixmap () const

返回光标像素图。这才有效,若光标是像素图光标。

[static] QPoint QCursor:: pos ()

Returns the position of the cursor (hot spot) in global screen coordinates.

可以调用 QWidget::mapFromGlobal () 以将其翻译成 Widget 坐标。

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

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

Moves the cursor (hot spot) to the global screen position ( x , y ).

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

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

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

这是重载函数。

把光标 (热点) 移动全局屏幕位置,在点 p .

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

将光标设为标识形状通过 shape .

Qt::CursorShape 了解光标形状列表。

另请参阅 shape ().

Qt::CursorShape QCursor:: shape () const

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

另请参阅 setShape ().

QCursor:: operator QVariant () const

返回光标作为 QVariant .

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

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

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

相关非成员

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

写入 cursor stream .

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

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

读取 cursor stream .

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