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. How to get the Qpoint of the widget by mouse click
Forum Update on Monday, May 27th 2025

How to get the Qpoint of the widget by mouse click

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 949 Views
  • 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.
  • S Offline
    S Offline
    summit
    wrote on last edited by summit
    #1

    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);
    
    		}
    	}
    `![everest_IMG.png](https://ddgobkiprc33d.cloudfront.net/c43a3818-d186-4fe9-9e6a-31c0df8c6f36.png) 
    
    The black region is the widget.
    
    If i click on the left corner of the widget than i should get (0.0 , 0.0)
    KroMignonK 1 Reply Last reply
    0
    • KroMignonK KroMignon

      @summit Did you try to use QWidget::mapFromGlobal() ?
      => https://doc.qt.io/qt-5/qwidget.html#mapFromGlobal

      S Offline
      S Offline
      summit
      wrote on last edited by
      #3

      @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.

      KroMignonK 1 Reply Last reply
      0
      • S summit

        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);
        
        		}
        	}
        `![everest_IMG.png](https://ddgobkiprc33d.cloudfront.net/c43a3818-d186-4fe9-9e6a-31c0df8c6f36.png) 
        
        The black region is the widget.
        
        If i click on the left corner of the widget than i should get (0.0 , 0.0)
        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #2

        @summit Did you try to use QWidget::mapFromGlobal() ?
        => https://doc.qt.io/qt-5/qwidget.html#mapFromGlobal

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        S 1 Reply Last reply
        0
        • KroMignonK KroMignon

          @summit Did you try to use QWidget::mapFromGlobal() ?
          => https://doc.qt.io/qt-5/qwidget.html#mapFromGlobal

          S Offline
          S Offline
          summit
          wrote on last edited by
          #3

          @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.

          KroMignonK 1 Reply Last reply
          0
          • S summit

            @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.

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #4

            @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 global QWidget::mapFromGlobal() should do the job, if they are from another widget QWidget::mapFrom() should be used.

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            S 1 Reply Last reply
            0
            • KroMignonK KroMignon

              @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 global QWidget::mapFromGlobal() should do the job, if they are from another widget QWidget::mapFrom() should be used.

              S Offline
              S Offline
              summit
              wrote on last edited by
              #5
              This post is deleted!
              KroMignonK 1 Reply Last reply
              0
              • S summit

                This post is deleted!

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by
                #6

                @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));
                

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                2

                • Login

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