Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QWidget QMouseEvent position() reporting relative to centralWidget
Forum Updated to NodeBB v4.3 + New Features

QWidget QMouseEvent position() reporting relative to centralWidget

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 5 Posters 164 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dabble53
    wrote last edited by
    #1

    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?

    Pl45m4P 1 Reply Last reply
    0
    • Kent-DorfmanK Kent-Dorfman

      Use < QWidget::setMouseTracking()> in your widget class, otherwise the parent will receive the event, not the child.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote last edited by
      #4

      @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() not mouseMoveEvent(), so why should this apply here?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote last edited by
        #2

        Hi,

        Which desktop environment are you using ?
        Which window manager ?
        Can you provide a minimal compilable example that shows this behavior ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote last edited by
          #3

          Use < QWidget::setMouseTracking()> in your widget class, otherwise the parent will receive the event, not the child.

          JonBJ 1 Reply Last reply
          0
          • Kent-DorfmanK Kent-Dorfman

            Use < QWidget::setMouseTracking()> in your widget class, otherwise the parent will receive the event, not the child.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote last edited by
            #4

            @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() not mouseMoveEvent(), so why should this apply here?

            1 Reply Last reply
            0
            • D dabble53

              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?

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote last edited by Pl45m4
              #5

              @dabble53

              Simple:
              Your point is in parent coordinates. And parent is the centralWidget to which your widget is located at 40,40... exactly the values to pass to it.

              So how do you print these numbers?


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              0
              • D dabble53 has marked this topic as solved

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved