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.
Forum Updated to NodeBB v4.3 + New Features

MouseMove event only work if mouse button is pressed.

Scheduled Pinned Locked Moved General and Desktop
12 Posts 7 Posters 24.5k 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.
  • A Offline
    A Offline
    antonyprojr
    wrote on 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 last edited by
      #2

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

      Try these in the constructor.

      R 1 Reply Last reply
      0
      • L Offline
        L Offline
        leon.anavi
        wrote on 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 last edited by
          #4

          you must setMouseTracking(true);

          1 Reply Last reply
          0
          • A Offline
            A Offline
            antonyprojr
            wrote on last edited by
            #5

            setMouseTracking(true) doesn't work!

            1 Reply Last reply
            0
            • A Offline
              A Offline
              antonyprojr
              wrote on 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

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

                Try these in the constructor.

                R Offline
                R Offline
                rohit1729
                wrote on last edited by rohit1729
                #7

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

                jsulmJ 1 Reply Last reply
                0
                • R rohit1729

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

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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
                  1
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @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 last edited by
                      #10

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

                      jsulmJ mrjjM 2 Replies Last reply
                      0
                      • R rohit1729

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

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 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

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

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 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