QWidget QMouseEvent position() reporting relative to centralWidget
-
I'm developing a custom widget derived from QWidget. (Linux/Fedora-42/g++/qt 6.9)
class MyWidget : public QWidget
{ ... usual stuff ...
void mousePressEvent(QMouseEvent*) override;
MyWidget(QWidget* parent, QRect geometry) : QWidget(parent)
{
setGeometry(geometry);
setFocusPolicy(Qt::StrongFocus);
};
I create in a MainWindow (button) click event
MyWidget* p = new MyWidget(ui->centralwidget, QRect(40, 40, 100, 100);
p->show();When I click on the displayed widget, the mouse event is giving me
coordinates relative to centralWidget. E.g., if I click on the upper left
corner, the mouse event position() is reporting (40, 40), not the (0,0)
I would expect.
Or am I reading the documentation wrong that says the coordinates
should be relative to MyWidget? -
Use < QWidget::setMouseTracking()> in your widget class, otherwise the parent will receive the event, not the child.
@Kent-Dorfman
I was just looking at this, as that is what it sounds like. But see here, the docs https://doc.qt.io/qt-6/qmouseevent.html#details state:Mouse move events will occur only when a mouse button is pressed down, unless mouse tracking has been enabled with QWidget::setMouseTracking().
So OP is calling
mousePressEvent()
notmouseMoveEvent()
, so why should this apply here? -
Hi,
Which desktop environment are you using ?
Which window manager ?
Can you provide a minimal compilable example that shows this behavior ? -
Use < QWidget::setMouseTracking()> in your widget class, otherwise the parent will receive the event, not the child.
-
Use < QWidget::setMouseTracking()> in your widget class, otherwise the parent will receive the event, not the child.
@Kent-Dorfman
I was just looking at this, as that is what it sounds like. But see here, the docs https://doc.qt.io/qt-6/qmouseevent.html#details state:Mouse move events will occur only when a mouse button is pressed down, unless mouse tracking has been enabled with QWidget::setMouseTracking().
So OP is calling
mousePressEvent()
notmouseMoveEvent()
, so why should this apply here? -
I'm developing a custom widget derived from QWidget. (Linux/Fedora-42/g++/qt 6.9)
class MyWidget : public QWidget
{ ... usual stuff ...
void mousePressEvent(QMouseEvent*) override;
MyWidget(QWidget* parent, QRect geometry) : QWidget(parent)
{
setGeometry(geometry);
setFocusPolicy(Qt::StrongFocus);
};
I create in a MainWindow (button) click event
MyWidget* p = new MyWidget(ui->centralwidget, QRect(40, 40, 100, 100);
p->show();When I click on the displayed widget, the mouse event is giving me
coordinates relative to centralWidget. E.g., if I click on the upper left
corner, the mouse event position() is reporting (40, 40), not the (0,0)
I would expect.
Or am I reading the documentation wrong that says the coordinates
should be relative to MyWidget? -