157QPoint QCursor::pos(
const QScreen *screen)
160 if (
const QPlatformCursor *cursor = screen->handle()->cursor()) {
161 const QPlatformScreen *ps = screen->handle();
162 QPoint nativePos = cursor->pos();
163 ps = ps->screenForPosition(nativePos);
164 return QHighDpi::fromNativePixels(nativePos, ps->screen());
167 return QGuiApplicationPrivate::lastCursorPosition.toPoint();
214void QCursor::setPos(QScreen *screen,
int x,
int y)
217 if (QPlatformCursor *cursor = screen->handle()->cursor()) {
218 const QPoint pos(x, y);
219 const QPoint devicePos = QHighDpi::toNativePixels(pos, screen->virtualSiblingAt(pos));
223 if (devicePos != cursor->pos())
224 cursor->setPos(devicePos);
281QDataStream &operator<<(QDataStream &s,
const QCursor &c)
283 s << (qint16)c.shape();
284 if (c.shape() == Qt::BitmapCursor) {
285 bool isPixmap =
false;
286 if (s.version() >= 7) {
287 isPixmap = !c.pixmap().isNull();
293 s << c.bitmap() << c.mask();
312 if (shape == Qt::BitmapCursor) {
313 bool isPixmap =
false;
314 if (s.version() >= 7)
320 c = QCursor(pm, hot.x(), hot.y());
324 s >> bm >> bmm >> hot;
325 c = QCursor(bm, bmm, hot.x(), hot.y());
328 c.setShape((Qt::CursorShape)shape);
353QCursor::QCursor(
const QPixmap &pixmap,
int hotX,
int hotY)
356 QImage img = pixmap.toImage().convertToFormat(QImage::Format_Indexed8, Qt::ThresholdDither|Qt::AvoidDither);
357 QBitmap bm = QBitmap::fromImage(img, Qt::ThresholdDither|Qt::AvoidDither);
358 QBitmap bmm = pixmap.mask();
363 else if (!pixmap.mask().isNull()) {
364 QImage mimg = pixmap.mask().toImage().convertToFormat(QImage::Format_Indexed8, Qt::ThresholdDither|Qt::AvoidDither);
365 bmm = QBitmap::fromImage(mimg, Qt::ThresholdDither|Qt::AvoidDither);
368 bmm = QBitmap(bm.size());
369 bmm.fill(Qt::color1);
372 d = QCursorData::setBitmap(bm, bmm, hotX, hotY, pixmap.devicePixelRatio());
462bool operator==(
const QCursor &lhs,
const QCursor &rhs)
noexcept
469 if (lhs.shape() == Qt::BitmapCursor && rhs.shape() == Qt::BitmapCursor
470 && lhs.hotSpot() == rhs.hotSpot()) {
471 if (!lhs.d->pixmap.isNull())
472 return lhs.d->pixmap.cacheKey() == rhs.d->pixmap.cacheKey();
474 if (!rhs.d->pixmap.isNull())
477 return lhs.d->bm->cacheKey() == rhs.d->bm->cacheKey()
478 && lhs.d->bmm->cacheKey() == rhs.d->bmm->cacheKey();
690void QCursorData::initialize()
692 if (QCursorData::initialized)
694 for (
int shape = 0; shape <= Qt::LastCursor; ++shape)
695 qt_cursorTable[shape] =
new QCursorData((Qt::CursorShape)shape);
696 QCursorData::initialized =
true;
699QCursorData *QCursorData::setBitmap(
const QBitmap &bitmap,
const QBitmap &mask,
int hotX,
int hotY, qreal devicePixelRatio)
701 QCursorData::initialize();
702 if (bitmap.depth() != 1 || mask.depth() != 1 || bitmap.size() != mask.size()) {
703 qWarning(
"QCursor: Cannot create bitmap cursor; invalid bitmap(s)");
704 QCursorData *c = qt_cursorTable[0];
708 QCursorData *d =
new QCursorData;
709 d->bm =
new QBitmap(bitmap);
710 d->bmm =
new QBitmap(mask);
711 d->cshape = Qt::BitmapCursor;
712 d->hx = hotX >= 0 ? hotX : bitmap.width() / 2 / devicePixelRatio;
713 d->hy = hotY >= 0 ? hotY : bitmap.height() / 2 / devicePixelRatio;