PDA

View Full Version : Widget dragging, widget doesn't fully draw



Meate
31st July 2012, 19:56
I created a widget that I can drag around the window. As the widget is being dragged, part of it doesn't draw fully, especially when dragging at faster speeds.

For example, while dragging the widget to the left, the left few pixels of the widget don't get drawn.

It's acting as if the widget is getting moved, but is clipped on the edge in the direction of the movement. This is a very simple widget and so moving/drawing it should be pretty quick (it's just a colored rectangle).

Basically, I'm just capturing a mousePressEvent, and then dragging the widget using move() based on the new mouse position in the mouseMoveEvent. I've tried various combinations of repaints and updates, but they don't seem to make any difference.

Any suggestions on how to make this more 'snappy'?


void PaletteControl::mousePressEvent(QMouseEvent* evt)
{
mouseDown = true;
mouseStartPoint = evt->pos();
}

void PaletteControl::mouseMoveEvent(QMouseEvent* evt)
{
if (mouseDown) {
mouseDragging = true;
}

if (mouseDragging) {
QPoint newPoint = evt->pos();
int deltaX = newPoint.x() - mouseStartPoint.x();
int deltaY = newPoint.y() - mouseStartPoint.y();

QPoint pos = this->pos();
move(pos.x() + deltaX, pos.y() + deltaY);
update();
}
}

amleto
31st July 2012, 20:11
use graphics view which will do this for you.

Meate
31st July 2012, 22:21
amleto, thank you for the suggestion. In fact, my widget eventually will contain controls and will be dragged around the QMainWindow, and so using the graphics view doesn't work for my purposes. I tried using a QDockWidget, but it has some undesired behaviors.

Added after 1 55 minutes:

I think I figured it out. It seems the parent widget needs to be told to update itself when one of its children is moved (outside of a layout). Calling parentWidget()->update() after the move() fixes the drawing issue.