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. Qt Mouse Move Event gives same position in few events
Forum Updated to NodeBB v4.3 + New Features

Qt Mouse Move Event gives same position in few events

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 2.6k Views 2 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.
  • S Offline
    S Offline
    Sivan
    wrote on last edited by
    #1

    Hi all,

    I am working on developing an application where I need to draw a line with the mouse movement. What I realised is that, the mouse moves faster than where the line appears if I am moving the mouse in a fast motion.

    Here is a sample code

    class MyGraphicsView : public QGraphicsView
    {
    public:
        typedef  QGraphicsView super;
        MyGraphicsView(QWidget* parent = NULL)
            : QGraphicsView(parent)
        {
            setViewport(new QOpenGLWidget);
            setBackgroundBrush(Qt::white);
    
            setScene(new QGraphicsScene);
        }
    
        virtual ~MyGraphicsView() {}
    
    protected:
        virtual void mouseMoveEvent(QMouseEvent* event)
        {
            super::mouseMoveEvent(event);
            if (event->buttons().testFlag(Qt::LeftButton))
            {
                mousePos = event->pos();
                qDebug() << "<<<<< event " << m_MousePos << event->localPos() << event->globalPos();
                viewport()->update();
            }
        }
    
        virtual void mouseReleaseEvent(QMouseEvent* event)
        {
            super::mouseReleaseEvent(event);
            mousePos = QPointF();
        }
    
        virtual void paintEvent(QPaintEvent* event)
        {
            if (!mousePos.isNull())
            {
                QPainter p(viewport());
                qDebug() << "Drawing";
                p.fillRect(QRectF(QPointF(m_MousePos.x(), 0), QSize(1, rect().height())), Qt::red);
            }
        }
    
    private:
        QPointF mousePos;
    };
    

    Here are my questions based on my observations:

    1. I noticed that the mouse move event getting called with the same pos(), localPos() and globalPos(). Why does this happens? If my understanding is correct, a mouse move event should happens when there is a change in the position
    2. When I move the mouse in a very slow motion (i.e 1 px movement) I notice that the cursor actually moves by 1 px, but the mouse event is getting triggered with the same previous position. I believe this could be the reason why I am not getting the result I want.

    Any idea on how I can solve this issue? Is it possible to achieve what I want in any other way?

    NOTE: There is no difference when I use QWidget instead of QGraphicsView. Just that when using QGraphicsView with QOpenGLWidget, I am getting better performance at with the rendering

    I am on Mac 10.12.2
    Qt 5.4.1

    Thanks in advance

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

      Hi,

      Good question, first thing you should do is to update your Qt version. Qt 5.4.1 is outdated, current release is 5.11, LTS is 5.9 with 5.12 (next LTS) around the corner.

      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
      • S Offline
        S Offline
        Sivan
        wrote on last edited by
        #3

        @SGaist , I have tried with Qt5.9.6. Still has the same issue

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

          You should take at look at the Scribble Example. It should give you a good starting point for custom paining using the mouse.

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

          S 1 Reply Last reply
          0
          • SGaistS SGaist

            You should take at look at the Scribble Example. It should give you a good starting point for custom paining using the mouse.

            S Offline
            S Offline
            Sivan
            wrote on last edited by
            #5

            @SGaist I did and it has the same issue I mentioned above.

            J.HilkJ 1 Reply Last reply
            0
            • S Sivan

              @SGaist I did and it has the same issue I mentioned above.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #6

              hi @Sivan

              IIRC macs can use a 2x/3x Display, where 2x2 or 3x3 pixels are grouped (logicaly) together.

              I think this could be the issue here. The mousemove event gets triggered, but the OS supplies the wrong logical position.
              There should be an option for that somewhere, but I'm not to versed in Qt for MacOS.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              S 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                hi @Sivan

                IIRC macs can use a 2x/3x Display, where 2x2 or 3x3 pixels are grouped (logicaly) together.

                I think this could be the issue here. The mousemove event gets triggered, but the OS supplies the wrong logical position.
                There should be an option for that somewhere, but I'm not to versed in Qt for MacOS.

                S Offline
                S Offline
                Sivan
                wrote on last edited by
                #7

                @J.Hilk That is interesting. I have tried to find this settings but I am not seeing anything along this line.

                J.HilkJ 1 Reply Last reply
                0
                • S Sivan

                  @J.Hilk That is interesting. I have tried to find this settings but I am not seeing anything along this line.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @Sivan
                  take a look here int he section about Operating System Support.

                  It may help.


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  1

                  • Login

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