I’ve looked at previous solutions like To monitor the cursor position and I wasn’t quite satisfied. At some point, I thought that I might have to add some API methods to Krita API, but it turns out the API as of 5.2.3 provides just enough to translate cursor position to pixel position. I was able to figure this out from the following references:
- libs/ui/widgets/KisScreenColorSampler.cpp · master · Graphics / Krita · GitLab
- libs/ui/canvas/kis_coordinates_converter.h · master · Graphics / Krita · GitLab
- libs/ui/canvas/kis_coordinates_converter.cpp · master · Graphics / Krita · GitLab
Some sample code that tracks pixel position on the top left of the canvas. The code can be run in Tools -> Scripts -> Scripter.
from krita import *
inst = Krita.instance()
win = inst.activeWindow()
qwin = win.qwindow()
view = win.activeView()
# Note 'view_0' is hard coded here.
# Krita increments the name for each new document. 'view_1', 'view_2', ...
# This line can fail.
# I recommend restarting Krita, opening a document, and then running the script.
pobj = qwin.findChild(QWidget,'view_0')
wobj = pobj.findChild(QOpenGLWidget)
def update():
flakeToImage = view.flakeToImageTransform()
widgetToFlake = view.flakeToCanvasTransform().inverted()[0]
widgetToImage = widgetToFlake * flakeToImage
gpos = QCursor.pos()
wpos = wobj.mapFromGlobal(gpos)
pos = widgetToImage.map(wpos)
x, y = pos.x(), pos.y()
label.setText(f'{x}x {y}y')
for child in wobj.children():
if isinstance(child, QLabel):
child._timer.stop()
child._timer.deleteLater()
child.deleteLater()
label = QLabel('999999x 999999y', wobj)
label.setEnabled(False)
label.setStyleSheet('background-color: rgba(0, 0, 0, 0.2); color: white;')
label.show()
timer = QTimer(label)
timer.timeout.connect(update)
timer.start(int(1000/60))