1

How to get the current mouse cursor size measured in pixels? I tried mywidget.cursor().pixmap().size() but it returns (0,0) for the standard arrow cursor.

(I need this to show a special tool tip label which would appear just below the cursor and would follow the cursor and I cannot use the standard QToolTip for certain reasons - delays etc. I already have a nice, working solution but if I display the label exactly at the cursor position, the cursor is painted over it hiding some text on the label. Of course I could move it down using some 'magic' number like 32 pixels, but this would cause me bad stomach feelings.)

2 Answers2

0

You can't do this with the standard cursors. The QCursor methods only work with custom bitmaps or pixmaps. So you will either have to use your own cursors, or estimate the size.

A quick web-search suggests that the standard cursors can vary in size and there is no fixed maximum (although that probably depends on the platform). For example, on X11, the size range usually includes 16, 24, 32, 48, and 64, but other sizes may be possible (even as large as 512). The default is normally 32.

If you need accuracy, it would seem that using custom cursors is the only way to solve this problem.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
0

You could use the code that is used in QTipLabel::placeTip, which offsets tooltips based on cursor size:

const int screenIndex = /*figure out what screen you are on*/;
const QScreen *screen = QGuiApplication::screens().value(screenIndex, QGuiApplication::primaryScreen());
if (const QPlatformScreen *platformScreen = screen ? screen->handle() : nullptr) {
    QPlatformCursor *cursor = platformScreen->cursor();
    const QSize nativeSize = cursor ? cursor->size() : QSize(16, 16);
    const QSize cursorSize = QHighDpi::fromNativePixels(nativeSize, platformScreen);
}

To do this you do need at least one private header:

#include <qpa/qplatformscreen.h>
#include <qpa/qplatformcursor.h>
#include <QtGui/private/qhighdpiscaling_p.h>

If it doesn't have to be portable you can look at the size implementation of the QPlatformCursor implementation for the platform you're targeting (e.g. QWindowsCursor::size()) and use that code.

Adversus
  • 2,166
  • 20
  • 23
  • This always returns 16×16 native size for me on X11 (Ubuntu 20.04 with Qt 5.15.0). Tested with 24px and 48px cursors. – Ruslan Jul 16 '22 at 22:34
  • @Ruslan you can check the qt sources, look for classes derived from QPlatformCursor, this specific implementation may be lacking. I only looked at the windows one which does get the right size. – Adversus Jul 18 '22 at 06:03