QCursor Class Reference

#include <qcursor.h>

Collaboration diagram for QCursor:

Collaboration graph
[legend]
List of all members.

Detailed Description

The QCursor class provides a mouse cursor with an arbitrary shape.

This class is mainly used to create mouse cursors that are associated with particular widgets and to get and set the position of the mouse cursor.

Qt has a number of standard cursor shapes, but you can also make custom cursor shapes based on a QBitmap, a mask and a hotspot.

To associate a cursor with a widget, use QWidget::setCursor(). To associate a cursor with all widgets (normally for a short period of time), use QApplication::setOverrideCursor().

To set a cursor shape use 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 enum.

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.

To set or get the position of the mouse cursor use the static methods QCursor::pos() and QCursor::setPos().

On X11, Qt supports the \link http://www.xfree86.org/4.3.0/Xcursor.3.html Xcursor\endlink library, which allows for full color icon themes. The table below shows the cursor name used for each Qt::CursorShape value. If a cursor cannot be found using the name shown below, a standard X11 cursor will be used instead. Note: X11 does not provide appropriate cursors for all possible Qt::CursorShape values. It is possible that some cursors will be taken from the Xcursor theme, while others will use an internal bitmap cursor. \table \header \o Shape \o Qt::CursorShape Value \o Cursor Name \o Shape \o Qt::CursorShape Value \o Cursor Name \row \o \inlineimage cursor-arrow.png \o Qt::ArrowCursor \o \c left_ptr \o \inlineimage cursor-sizev.png \o Qt::SizeVerCursor \o \c size_ver \row \o \inlineimage cursor-uparrow.png \o Qt::UpArrowCursor \o \c up_arrow \o \inlineimage cursor-sizeh.png \o Qt::SizeHorCursor \o \c size_hor \row \o \inlineimage cursor-cross.png \o Qt::CrossCursor \o \c cross \o \inlineimage cursor-sizeb.png \o Qt::SizeBDiagCursor \o \c size_bdiag \row \o \inlineimage cursor-ibeam.png \o Qt::IBeamCursor \o \c ibeam \o \inlineimage cursor-sizef.png \o Qt::SizeFDiagCursor \o \c size_fdiag \row \o \inlineimage cursor-wait.png \o Qt::WaitCursor \o \c wait \o \inlineimage cursor-sizeall.png \o Qt::SizeAllCursor \o \c size_all \row \o \inlineimage cursor-busy.png \o Qt::BusyCursor \o \c left_ptr_watch \o \inlineimage cursor-vsplit.png \o Qt::SplitVCursor \o \c split_v \row \o \inlineimage cursor-forbidden.png \o Qt::ForbiddenCursor \o \c forbidden \o \inlineimage cursor-hsplit.png \o Qt::SplitHCursor \o \c split_h \row \o \inlineimage cursor-hand.png \o Qt::PointingHandCursor \o \c pointing_hand \o \inlineimage cursor-openhand.png \o Qt::OpenHandCursor \o \c openhand \row \o \inlineimage cursor-whatsthis.png \o Qt::WhatsThisCursor \o \c whats_this \o \inlineimage cursor-closedhand.png \o Qt::ClosedHandCursor \o \c closedhand \endtable \sa QWidget, {fowler}{GUI Design Handbook: Cursors} Definition at line 63 of file qcursor.h.

Public Member Functions

 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 &cursor)
 ~QCursor ()
QCursoroperator= (const QCursor &cursor)
 operator QVariant () const
Qt::CursorShape shape () const
void setShape (Qt::CursorShape newShape)
const QBitmapbitmap () const
const QBitmapmask () const
QPixmap pixmap () const
QPoint hotSpot () const

Static Public Member Functions

static QPoint pos ()
static void setPos (int x, int y)
static void setPos (const QPoint &p)

Private Attributes

QCursorDatad

Related Functions

(Note that these are not member functions.)

QDataStreamoperator<< (QDataStream &stream, const QCursor &cursor)
QDataStreamoperator>> (QDataStream &stream, QCursor &cursor)


Constructor & Destructor Documentation

QCursor::QCursor (  ) 

Constructs a cursor with the default arrow shape.

Definition at line 335 of file qcursor.cpp.

References c, d, QCursorData::initialize(), QCursorData::initialized, qApp, and qt_cursorTable.

00336 {
00337     if (!QCursorData::initialized) {
00338         if (qApp->startingUp()) {
00339             d = 0;
00340             return;
00341         }
00342         QCursorData::initialize();
00343     }
00344     QCursorData *c = qt_cursorTable[0];
00345     c->ref.ref();
00346     d = c;
00347 }

Here is the call graph for this function:

QCursor::QCursor ( Qt::CursorShape  shape  ) 

Constructs a cursor with the specified shape.

See Qt::CursorShape for a list of shapes.

See also:
setShape()

Definition at line 356 of file qcursor.cpp.

References QCursorData::initialize(), QCursorData::initialized, and setShape().

00357     : d(0)
00358 {
00359     if (!QCursorData::initialized)
00360         QCursorData::initialize();
00361     setShape(shape);
00362 }

Here is the call graph for this function:

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

Constructs a custom bitmap cursor.

bitmap and mask make up the bitmap. hotX and hotY define the cursor's hot spot.

If hotX is negative, it is set to the {bitmap().width()/2}. If hotY is negative, it is set to the {bitmap().height()/2}.

The cursor bitmap (B) and mask (M) bits are combined like this: B=1 and M=1 gives black. B=0 and M=1 gives white. B=0 and M=0 gives transparent. B=1 and M=0 gives an XOR'd result.

Use the global Qt color Qt::color0 to draw 0-pixels and Qt::color1 to draw 1-pixels in the bitmaps.

Valid cursor sizes depend on the display hardware (or the underlying window system). We recommend using 32 x 32 cursors, because this size is supported on all platforms. Some platforms also support 16 x 16, 48 x 48, and 64 x 64 cursors.

See also:
QBitmap::QBitmap(), QBitmap::setMask()

Definition at line 297 of file qcursor.cpp.

References bitmap(), d, mask(), and QCursorData::setBitmap().

00298     : d(0)
00299 {
00300     d = QCursorData::setBitmap(bitmap, mask, hotX, hotY);
00301 }

Here is the call graph for this function:

QCursor::QCursor ( const QPixmap pixmap,
int  hotX = -1,
int  hotY = -1 
)

Constructs a custom pixmap cursor.

pixmap is the image. It is usual to give it a mask (set using QPixmap::setMask()). hotX and hotY define the cursor's hot spot.

If hotX is negative, it is set to the {pixmap().width()/2}. If hotY is negative, it is set to the {pixmap().height()/2}.

Valid cursor sizes depend on the display hardware (or the underlying window system). We recommend using 32 x 32 cursors, because this size is supported on all platforms. Some platforms also support 16 x 16, 48 x 48, and 64 x 64 cursors.

See also:
QPixmap::QPixmap(), QPixmap::setMask()

Definition at line 242 of file qcursor.cpp.

References Qt::AvoidDither, Qt::color1, QImage::convertToFormat(), d, QPixmap::fill(), QImage::Format_Indexed8, QBitmap::fromImage(), QPixmap::isNull(), QPixmap::mask(), pixmap(), QCursorData::pixmap, QCursorData::setBitmap(), QPixmap::setMask(), QPixmap::size(), Qt::ThresholdDither, and QPixmap::toImage().

00243     : d(0)
00244 {
00245     QImage img = pixmap.toImage().convertToFormat(QImage::Format_Indexed8, Qt::ThresholdDither|Qt::AvoidDither);
00246     QBitmap bm = QBitmap::fromImage(img, Qt::ThresholdDither|Qt::AvoidDither);
00247     QBitmap bmm = pixmap.mask();
00248     if (!bmm.isNull()) {
00249         QBitmap nullBm;
00250         bm.setMask(nullBm);
00251     }
00252     else if (!pixmap.mask().isNull()) {
00253         QImage mimg = pixmap.mask().toImage().convertToFormat(QImage::Format_Indexed8, Qt::ThresholdDither|Qt::AvoidDither);
00254         bmm = QBitmap::fromImage(mimg, Qt::ThresholdDither|Qt::AvoidDither);
00255     }
00256     else {
00257         bmm = QBitmap(bm.size());
00258         bmm.fill(Qt::color1);
00259     }
00260 
00261     d = QCursorData::setBitmap(bm, bmm, hotX, hotY);
00262     d->pixmap = pixmap;
00263 }

Here is the call graph for this function:

QCursor::QCursor ( const QCursor c  ) 

Constructs a copy of the cursor c.

Definition at line 453 of file qcursor.cpp.

References c, d, QCursorData::initialize(), QCursorData::initialized, QBasicAtomic::ref(), and QCursorData::ref.

00454 {
00455     if (!QCursorData::initialized)
00456         QCursorData::initialize();
00457     d = c.d;
00458     d->ref.ref();
00459 }

Here is the call graph for this function:

QCursor::~QCursor (  ) 

Destroys the cursor.

Definition at line 465 of file qcursor.cpp.

References d, QBasicAtomic::deref(), and QCursorData::ref.

00466 {
00467     if (d && !d->ref.deref())
00468         delete d;
00469 }

Here is the call graph for this function:


Member Function Documentation

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

Assigns c to this cursor and returns a reference to this cursor.

Definition at line 477 of file qcursor.cpp.

References c, d, QCursorData::initialize(), QCursorData::initialized, and qAtomicAssign().

00478 {
00479     if (!QCursorData::initialized)
00480         QCursorData::initialize();
00481     qAtomicAssign(d, c.d);
00482     return *this;
00483 }

Here is the call graph for this function:

QCursor::operator QVariant (  )  const

Returns the cursor as a QVariant.

Definition at line 488 of file qcursor.cpp.

References QVariant::Cursor.

00489 {
00490     return QVariant(QVariant::Cursor, this);
00491 }

Qt::CursorShape QCursor::shape (  )  const

Returns the cursor shape identifier. The return value is one of the Qt::CursorShape enum values (cast to an int).

See also:
setShape()

Definition at line 371 of file qcursor.cpp.

References QCursorData::cshape, d, QCursorData::initialize(), and QCursorData::initialized.

Referenced by QTextBrowserPrivate::_q_highlightLink(), qdesigner_internal::CursorProperty::decoration(), Q3TableHeader::mouseMoveEvent(), Q3TableHeader::mousePressEvent(), QWidget::setCursor(), qdesigner_internal::CursorProperty::toString(), qdesigner_internal::CursorProperty::updateEditorContents(), and qdesigner_internal::CursorProperty::updateValue().

00372 {
00373     if (!QCursorData::initialized)
00374         QCursorData::initialize();
00375     return d->cshape;
00376 }

Here is the call graph for this function:

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

Sets the cursor to the shape identified by shape.

See Qt::CursorShape for the list of cursor shapes.

See also:
shape()

Definition at line 385 of file qcursor.cpp.

References c, d, QCursorData::initialize(), QCursorData::initialized, Qt::LastCursor, qAtomicSetPtr(), and qt_cursorTable.

Referenced by QCursor().

00386 {
00387     if (!QCursorData::initialized)
00388         QCursorData::initialize();
00389     QCursorData *c = uint(shape) <= Qt::LastCursor ? qt_cursorTable[shape] : 0;
00390     if (!c)
00391         c = qt_cursorTable[0];
00392     c->ref.ref();
00393     if (!d) {
00394         d = c;
00395     } else {
00396         c = qAtomicSetPtr(&d, c);
00397         if (!c->ref.deref())
00398             delete c;
00399     }
00400 }

Here is the call graph for this function:

const QBitmap * QCursor::bitmap (  )  const

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

Definition at line 406 of file qcursor.cpp.

References QCursorData::bm, d, QCursorData::initialize(), and QCursorData::initialized.

Referenced by QCursor().

00407 {
00408     if (!QCursorData::initialized)
00409         QCursorData::initialize();
00410     return d->bm;
00411 }

Here is the call graph for this function:

const QBitmap * QCursor::mask (  )  const

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

Definition at line 418 of file qcursor.cpp.

References QCursorData::bmm, d, QCursorData::initialize(), and QCursorData::initialized.

Referenced by QCursor().

00419 {
00420     if (!QCursorData::initialized)
00421         QCursorData::initialize();
00422     return d->bmm;
00423 }

Here is the call graph for this function:

QPixmap QCursor::pixmap (  )  const

Returns the cursor pixmap. This is only valid if the cursor is a pixmap cursor.

Definition at line 430 of file qcursor.cpp.

References d, QCursorData::initialize(), QCursorData::initialized, and QCursorData::pixmap.

Referenced by QCursor().

00431 {
00432     if (!QCursorData::initialized)
00433         QCursorData::initialize();
00434     return d->pixmap;
00435 }

Here is the call graph for this function:

QPoint QCursor::hotSpot (  )  const

Returns the cursor hot spot, or (0, 0) if it is one of the standard cursors.

Definition at line 442 of file qcursor.cpp.

References d, QCursorData::hx, QCursorData::hy, QCursorData::initialize(), and QCursorData::initialized.

00443 {
00444     if (!QCursorData::initialized)
00445         QCursorData::initialize();
00446     return QPoint(d->hx, d->hy);
00447 }

Here is the call graph for this function:

QPoint QCursor::pos (  )  [static]

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

You can call QWidget::mapFromGlobal() to translate it to widget coordinates.

See also:
setPos(), QWidget::mapFromGlobal(), QWidget::mapToGlobal()

Definition at line 128 of file qcursor_x11.cpp.

References QX11Info::appRootWindow(), i, and X11.

Referenced by Q3AccelPrivate::activate(), QDialog::adjustPosition(), Q3TextEdit::autoScrollTimerDone(), QGraphicsViewPrivate::centerView(), QApplicationPrivate::dispatchEnterLeave(), Q3ListView::doAutoScroll(), QAbstractItemView::doAutoScroll(), Q3TableHeader::doAutoScroll(), Q3IconView::doAutoScroll(), Q3Table::doAutoScroll(), Q3ScrollView::doDragAutoScroll(), QWidgetResizeHandler::doMove(), QWidgetResizeHandler::doResize(), QDragManager::drag(), qdesigner_internal::FormWindowManager::dragItems(), qdesigner_internal::ConnectionEdit::endConnection(), qdesigner_internal::BuddyEditor::endConnection(), QShortcut::event(), QDragManager::eventFilter(), QPixelTool::grabScreen(), qdesigner_internal::WidgetBoxTreeView::handleMousePress(), QMenu::internalDelayedPopup(), Q3DockWindowTitleBar::keyPressEvent(), QWidgetResizeHandler::keyPressEvent(), Q3DockWindowHandle::keyPressEvent(), Q3DockWindowHandle::keyReleaseEvent(), Q3DockWindowTitleBar::keyReleaseEvent(), QApplicationPrivate::leaveModal_sys(), QGraphicsScenePrivate::leaveScene(), QDragManager::move(), Q3TextEdit::optimDoAutoScroll(), Q3TitleBar::paintEvent(), QWorkspaceTitleBar::paintEvent(), QMenu::popup(), Q3FileDialog::popupContextMenu(), QVFb::popupMenu(), QContextMenuEvent::QContextMenuEvent(), QMouseEvent::QMouseEvent(), QWhatsThisPrivate::QWhatsThisPrivate(), QWheelEvent::QWheelEvent(), QGraphicsItem::setCursor(), QHeaderView::setOffset(), QTextBrowserPrivate::setSource(), Q3TextBrowser::setSource(), MainWindow::setupPuzzle(), QAbstractItemView::startDrag(), QDragManager::timerEvent(), QTextEdit::timerEvent(), QDragManager::updatePixmap(), and QMessageBoxPrivate::updateSize().

00129 {
00130     Window root;
00131     Window child;
00132     int root_x, root_y, win_x, win_y;
00133     uint buttons;
00134     Display* dpy = X11->display;
00135     for (int i = 0; i < ScreenCount(dpy); ++i) {
00136         if (XQueryPointer(dpy, QX11Info::appRootWindow(i), &root, &child, &root_x, &root_y,
00137                           &win_x, &win_y, &buttons))
00138 
00139             return QPoint(root_x, root_y);
00140     }
00141     return QPoint();
00142 }

Here is the call graph for this function:

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

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

You can call QWidget::mapToGlobal() to translate widget coordinates to global screen coordinates.

See also:
pos(), QWidget::mapFromGlobal(), QWidget::mapToGlobal()

Definition at line 171 of file qcursor_x11.cpp.

References QX11Info::appRootWindow(), X11, and XNone.

Referenced by QWidgetResizeHandler::keyPressEvent(), QTest::mouseEvent(), and QHeaderView::setOffset().

00172 {
00173     QPoint current, target(x, y);
00174 
00175     // this is copied from pos(), since we need the screen number for the correct
00176     // root window in the XWarpPointer call
00177     Window root;
00178     Window child;
00179     int root_x, root_y, win_x, win_y;
00180     uint buttons;
00181     Display* dpy = X11->display;
00182     int screen;
00183     for (screen = 0; screen < ScreenCount(dpy); ++screen) {
00184         if (XQueryPointer(dpy, QX11Info::appRootWindow(screen), &root, &child, &root_x, &root_y,
00185                           &win_x, &win_y, &buttons)) {
00186             current = QPoint(root_x, root_y);
00187             break;
00188         }
00189     }
00190 
00191     if (screen >= ScreenCount(dpy))
00192         return;
00193 
00194     // Need to check, since some X servers generate null mouse move
00195     // events, causing looping in applications which call setPos() on
00196     // every mouse move event.
00197     //
00198     if (current == target)
00199         return;
00200 
00201     XWarpPointer(X11->display, XNone, QX11Info::appRootWindow(screen), 0, 0, 0, 0, x, y);
00202 }

Here is the call graph for this function:

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Moves the cursor (hot spot) to the global screen position at point p.

Definition at line 85 of file qcursor.h.

References p.

00085 { setPos(p.x(), p.y()); }


Friends And Related Function Documentation

QDataStream & operator<< ( QDataStream stream,
const QCursor cursor 
) [related]

Writes the cursor to the stream.

See also:
{Format of the QDataStream operators}

Definition at line 170 of file qcursor.cpp.

00171 {
00172     s << (qint16)c.shape();                        // write shape id to stream
00173     if (c.shape() == Qt::BitmapCursor) {                // bitmap cursor
00174         bool isPixmap = false;
00175         if (s.version() >= 7) {
00176             isPixmap = !c.pixmap().isNull();
00177             s << isPixmap;
00178         }
00179         if (isPixmap)
00180             s << c.pixmap();
00181         else
00182             s << *c.bitmap() << *c.mask();
00183         s << c.hotSpot();
00184     }
00185     return s;
00186 }

QDataStream & operator>> ( QDataStream stream,
QCursor cursor 
) [related]

Reads the cursor from the stream.

See also:
{Format of the QDataStream operators}

Definition at line 197 of file qcursor.cpp.

00198 {
00199     qint16 shape;
00200     s >> shape;                                        // read shape id from stream
00201     if (shape == Qt::BitmapCursor) {                // read bitmap cursor
00202         bool isPixmap = false;
00203         if (s.version() >= 7)
00204             s >> isPixmap;
00205         if (isPixmap) {
00206             QPixmap pm;
00207             QPoint hot;
00208             s >> pm >> hot;
00209             c = QCursor(pm, hot.x(), hot.y());
00210         } else {
00211             QBitmap bm, bmm;
00212             QPoint hot;
00213             s >> bm >> bmm >> hot;
00214             c = QCursor(bm, bmm, hot.x(), hot.y());
00215         }
00216     } else {
00217         c.setShape((Qt::CursorShape)shape);                // create cursor with shape
00218     }
00219     return s;
00220 }


Member Data Documentation

QCursorData* QCursor::d [private]

Definition at line 109 of file qcursor.h.

Referenced by bitmap(), hotSpot(), mask(), operator=(), pixmap(), QCursor(), setShape(), shape(), and ~QCursor().


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 17:06:12 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1