156QPoint QCursor::pos(
const QScreen *screen)
159 if (
const QPlatformCursor *cursor = screen->handle()->cursor()) {
160 const QPlatformScreen *ps = screen->handle();
161 QPoint nativePos = cursor->pos();
162 ps = ps->screenForPosition(nativePos);
163 return QHighDpi::fromNativePixels(nativePos, ps->screen());
166 return QGuiApplicationPrivate::lastCursorPosition.toPoint();
213void QCursor::setPos(QScreen *screen,
int x,
int y)
216 if (QPlatformCursor *cursor = screen->handle()->cursor()) {
217 const QPoint pos(x, y);
218 const QPoint devicePos = QHighDpi::toNativePixels(pos, screen->virtualSiblingAt(pos));
222 if (devicePos != cursor->pos())
223 cursor->setPos(devicePos);
280QDataStream &operator<<(QDataStream &s,
const QCursor &c)
282 s << (qint16)c.shape();
283 if (c.shape() == Qt::BitmapCursor) {
284 bool isPixmap =
false;
285 if (s.version() >= 7) {
286 isPixmap = !c.pixmap().isNull();
292 s << c.bitmap() << c.mask();
311 if (shape == Qt::BitmapCursor) {
312 bool isPixmap =
false;
313 if (s.version() >= 7)
319 c = QCursor(pm, hot.x(), hot.y());
323 s >> bm >> bmm >> hot;
324 c = QCursor(bm, bmm, hot.x(), hot.y());
327 c.setShape((Qt::CursorShape)shape);
352QCursor::QCursor(
const QPixmap &pixmap,
int hotX,
int hotY)
355 QImage img = pixmap.toImage().convertToFormat(QImage::Format_Indexed8, Qt::ThresholdDither|Qt::AvoidDither);
356 QBitmap bm = QBitmap::fromImage(img, Qt::ThresholdDither|Qt::AvoidDither);
357 QBitmap bmm = pixmap.mask();
362 else if (!pixmap.mask().isNull()) {
363 QImage mimg = pixmap.mask().toImage().convertToFormat(QImage::Format_Indexed8, Qt::ThresholdDither|Qt::AvoidDither);
364 bmm = QBitmap::fromImage(mimg, Qt::ThresholdDither|Qt::AvoidDither);
367 bmm = QBitmap(bm.size());
368 bmm.fill(Qt::color1);
371 d = QCursorData::setBitmap(bm, bmm, hotX, hotY, pixmap.devicePixelRatio());
461bool operator==(
const QCursor &lhs,
const QCursor &rhs)
noexcept
468 if (lhs.shape() == Qt::BitmapCursor && rhs.shape() == Qt::BitmapCursor
469 && lhs.hotSpot() == rhs.hotSpot()) {
470 if (!lhs.d->pixmap.isNull())
471 return lhs.d->pixmap.cacheKey() == rhs.d->pixmap.cacheKey();
473 if (!rhs.d->pixmap.isNull())
476 return lhs.d->bm->cacheKey() == rhs.d->bm->cacheKey()
477 && lhs.d->bmm->cacheKey() == rhs.d->bmm->cacheKey();
689void QCursorData::initialize()
691 if (QCursorData::initialized)
693 for (
int shape = 0; shape <= Qt::LastCursor; ++shape)
694 qt_cursorTable[shape] =
new QCursorData((Qt::CursorShape)shape);
695 QCursorData::initialized =
true;
698QCursorData *QCursorData::setBitmap(
const QBitmap &bitmap,
const QBitmap &mask,
int hotX,
int hotY, qreal devicePixelRatio)
700 QCursorData::initialize();
701 if (bitmap.depth() != 1 || mask.depth() != 1 || bitmap.size() != mask.size()) {
702 qWarning(
"QCursor: Cannot create bitmap cursor; invalid bitmap(s)");
703 QCursorData *c = qt_cursorTable[0];
707 QCursorData *d =
new QCursorData;
708 d->bm =
new QBitmap(bitmap);
709 d->bmm =
new QBitmap(mask);
710 d->cshape = Qt::BitmapCursor;
711 d->hx = hotX >= 0 ? hotX : bitmap.width() / 2 / devicePixelRatio;
712 d->hy = hotY >= 0 ? hotY : bitmap.height() / 2 / devicePixelRatio;