If all you want to do is tell the mouse position in coordinates, as if the lower left corner of the widget was (0,0), and Y increased as you climb, then the code below does this. I think that the reason for the desire for such a code is wrong, since the coordinates of everything else in the specified widget do not work in this way. So why do you want it, I canโt understand, but here you go.
#include <QApplication> #include <QMouseEvent> #include <QTransform> #include <QLabel> class Window : public QLabel { public: Window(QWidget *parent = 0, Qt::WindowFlags f = 0) : QLabel(parent, f) { setMouseTracking(true); setMinimumSize(100, 100); } void mouseMoveEvent(QMouseEvent *ev) { // vvv That where the magic happens QTransform t; t.scale(1, -1); t.translate(0, -height()+1); QPoint pos = ev->pos() * t; // ^^^ setText(QString("%1, %2").arg(pos.x()).arg(pos.y())); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); Window w; w.show(); return a.exec(); }
source share