How to get the Qpoint of the widget by mouse click
-
I have a widget and when i click my mouse on the widget i need to get the Qpoint local to the widget.
if i click on the left corner of the widget than the value should be 0 , 0.
This is my code.
if (event->button() == Qt::LeftButton) { if (widgetRender->underMouse()) { lastPoint = event->pos(); QPoint point = widgetRender->mapFromGlobal(lastPoint); Sum_ScreenSpaceToWorldSpaceCoords ScreenToWorldCoords; SumPoint p; p.x = point.x(); p.y = point.y(); ScreenToWorldCoords.PrintMousePos(p); } } ` The black region is the widget. If i click on the left corner of the widget than i should get (0.0 , 0.0)
-
@summit Did you try to use
QWidget::mapFromGlobal()
?
=> https://doc.qt.io/qt-5/qwidget.html#mapFromGlobal@KroMignon i have updated my code where i am using mapfromGlobal but i do not get 0,0 when i click on the left corner of the widget.
-
I have a widget and when i click my mouse on the widget i need to get the Qpoint local to the widget.
if i click on the left corner of the widget than the value should be 0 , 0.
This is my code.
if (event->button() == Qt::LeftButton) { if (widgetRender->underMouse()) { lastPoint = event->pos(); QPoint point = widgetRender->mapFromGlobal(lastPoint); Sum_ScreenSpaceToWorldSpaceCoords ScreenToWorldCoords; SumPoint p; p.x = point.x(); p.y = point.y(); ScreenToWorldCoords.PrintMousePos(p); } } ` The black region is the widget. If i click on the left corner of the widget than i should get (0.0 , 0.0)
-
@summit Did you try to use
QWidget::mapFromGlobal()
?
=> https://doc.qt.io/qt-5/qwidget.html#mapFromGlobal@KroMignon i have updated my code where i am using mapfromGlobal but i do not get 0,0 when i click on the left corner of the widget.
-
@KroMignon i have updated my code where i am using mapfromGlobal but i do not get 0,0 when i click on the left corner of the widget.
@summit I have some difficulties to understand what exactly you are doing.
From where did you get the mouse event?
Are those coordinates global or are they from another QWidget?
If they are globalQWidget::mapFromGlobal()
should do the job, if they are from another widgetQWidget::mapFrom()
should be used. -
@summit I have some difficulties to understand what exactly you are doing.
From where did you get the mouse event?
Are those coordinates global or are they from another QWidget?
If they are globalQWidget::mapFromGlobal()
should do the job, if they are from another widgetQWidget::mapFrom()
should be used. -
@summit said in How to get the Qpoint of the widget by mouse click:
The mouse coordinates are global coordinates from the
void mousePressEvent(QMouseEvent *event)Sorry, my question was not clear: how do you register this function to get mouse events?
I am not a QWidget expert, so maybe I am wrong.The way I would do it is to add a global event listener to get mouse events:
- create a QObject class to handle the event
class MouseEventFilter : public QObject { Q_OBJECT public: explicit MouseEventFilter(QObject *parent = 0); public: bool eventFilter(QObject *object, QEvent *event); };
- register this class app to get all events
qApp->installEventFilter(new MouseEventFilter(qApp));