Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

c++ - Qt, Mouse skipping, not updating every pixel, mouseMoveEvent()

I working on a simple paint program. It seemed Qt (and KDE) would be a easy way to implement it. I find Qt quite easy to work with, but now I have hit a problem.

When I draw something in my program the mouse skips if I move the mouse to fast.

like this:
alt text
It susposed to be like one long string.

I'm using mouseMoveEvent() to draw a pixel to my image when the left mouse button is pressed down. I have called setMouseTracking(true); so the event should be called as long as I move the mouse.

void camoMaker::mouseMoveEvent(QMouseEvent *ev)
{
    if(ev->state()==Qt::LeftButton)
    {
        QPoint mPoint=ev->pos();
        mPoint.setX(mPoint.x()-80);
        drawPoint(mPoint);
    }
}

camoMaker is the main widget.
drawPoint() draws a pixel on both a internal QImage and using QPainter on a QWidget thats the drawing area.

It seems to me that either mouseMoveEvent() isn't called for every pixel the mouse moves or that the mouse actually just skips some pixel.

I understand that it might just how it works and not Qt fault but X11 or how the OS handle mouse position/input.

If so how would I go about to fix it, should I try to interpolate from 2 points that gets registered?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Mouse events don't occur for each pixel as the mouse moves, on most operating systems. The message handlers (including KDE/linux) repeatedly show mouse movements, but pixels will often be skipped.

You'll need to track the last pixel location, and either draw a line, or add extra points in between the last position and the current position.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...