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. MouseMove event only work if mouse button is pressed.
QtWS25 Last Chance

MouseMove event only work if mouse button is pressed.

Scheduled Pinned Locked Moved General and Desktop
12 Posts 7 Posters 24.4k 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.
  • A Offline
    A Offline
    antonyprojr
    wrote on 4 Oct 2012, 16:26 last edited by
    #1

    I'm trying to make some code that get mouse coordinates when mouse is moved inside MainWindow, the code below only work if some mouse button is pressed, how can i make it work even if no mouse button is pressed? I tried "setMouseTracking(true)" but it not work!

    @void MainWindow::mouseMoveEvent(QMouseEvent* event)
    {
    int x = event->x();
    int y = event->y();
    }@

    1 Reply Last reply
    0
    • T Offline
      T Offline
      TFabbri
      wrote on 4 Oct 2012, 16:46 last edited by
      #2

      @
      m_scene = new QGraphicsScene(this);
      setScene(m_scene);
      setMouseTracking(true);
      @

      Try these in the constructor.

      R 1 Reply Last reply 24 Jul 2019, 07:27
      0
      • L Offline
        L Offline
        leon.anavi
        wrote on 4 Oct 2012, 17:44 last edited by
        #3

        Use the static method "QCursor::pos()":http://qt-project.org/doc/qt-4.8/qcursor.html#pos to get the position of the cursor in global screen coordinates.

        http://anavi.org/

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Ali Sarlak
          wrote on 4 Oct 2012, 20:01 last edited by
          #4

          you must setMouseTracking(true);

          1 Reply Last reply
          0
          • A Offline
            A Offline
            antonyprojr
            wrote on 5 Oct 2012, 19:20 last edited by
            #5

            setMouseTracking(true) doesn't work!

            1 Reply Last reply
            0
            • A Offline
              A Offline
              antonyprojr
              wrote on 6 Oct 2012, 19:05 last edited by
              #6

              Now I’m using mouse hook to get the mouse coordinates, but i'm trying to display it on my label and the code don’t work.

              @LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
              {
              if(nCode==HC_ACTION)
              {
              POINT p;
              GetCursorPos(&p);
              MainWindow* mw = new MainWindow();
              mw->ui->label->setText(QString::number(p.x)+"|"+QString::number(p.y));//this code don't work!
              }
              return CallNextHookEx(NULL,nCode,wParam,lParam);
              }
              @

              How can i display the coordinates in the label?

              1 Reply Last reply
              0
              • T TFabbri
                4 Oct 2012, 16:46

                @
                m_scene = new QGraphicsScene(this);
                setScene(m_scene);
                setMouseTracking(true);
                @

                Try these in the constructor.

                R Offline
                R Offline
                rohit1729
                wrote on 24 Jul 2019, 07:27 last edited by rohit1729
                #7

                @TFabbri error: C2065: 'm_scene': undeclared identifier. C3861: 'setScene': identifier not found.
                How do I solve it?

                J 1 Reply Last reply 24 Jul 2019, 07:37
                0
                • R rohit1729
                  24 Jul 2019, 07:27

                  @TFabbri error: C2065: 'm_scene': undeclared identifier. C3861: 'setScene': identifier not found.
                  How do I solve it?

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 24 Jul 2019, 07:37 last edited by
                  #8

                  @rohit1729 This thread is 7 years old already!
                  The error tells you that m_scene is not declared. So, did you declare it?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  R 1 Reply Last reply 24 Jul 2019, 08:26
                  1
                  • M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 24 Jul 2019, 07:39 last edited by
                    #9

                    Hi
                    its just a member in .h
                    QGraphicsScene* m_scene;

                    1 Reply Last reply
                    0
                    • J jsulm
                      24 Jul 2019, 07:37

                      @rohit1729 This thread is 7 years old already!
                      The error tells you that m_scene is not declared. So, did you declare it?

                      R Offline
                      R Offline
                      rohit1729
                      wrote on 24 Jul 2019, 08:26 last edited by
                      #10

                      @jsulm Yeah i did. Still the error is 'setScene': identifier not found.

                      J M 2 Replies Last reply 24 Jul 2019, 08:30
                      0
                      • R rohit1729
                        24 Jul 2019, 08:26

                        @jsulm Yeah i did. Still the error is 'setScene': identifier not found.

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 24 Jul 2019, 08:30 last edited by
                        #11

                        @rohit1729 Well, you did not declare setScene either...

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        3
                        • R rohit1729
                          24 Jul 2019, 08:26

                          @jsulm Yeah i did. Still the error is 'setScene': identifier not found.

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 24 Jul 2019, 08:31 last edited by
                          #12

                          @rohit1729
                          hi
                          that is because it belongs to
                          https://doc.qt.io/qt-5/qgraphicsview.html#setScene
                          and unless you in a qgraphicsview subclass, it wont have that method.

                          You can just create a new view, if you dont already uses one.

                          1 Reply Last reply
                          5

                          • Login

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