Qt Mouse Move Event gives same position in few events
-
wrote on 5 Oct 2018, 05:24 last edited by
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:
- 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
- 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.1Thanks in advance
-
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.
-
You should take at look at the Scribble Example. It should give you a good starting point for custom paining using the mouse.
-
You should take at look at the Scribble Example. It should give you a good starting point for custom paining using the mouse.
-
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. -
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. -
@J.Hilk That is interesting. I have tried to find this settings but I am not seeing anything along this line.
1/8