Caius Aérobus
5th April 2006, 15:30
Hello,
I just draw some rectangles in a painter and I want to be able to move the rectangles. The coordinates data are stored using a QAbstractItemModel.
I have a paint code as below:
void
vadControlPolygon::paintEvent(QPaintEvent *event)
{
QPainter painter(this->viewport());
painter.setWindow(0, 0, 100, 100);
QStyleOptionViewItem option;
painter.eraseRect(painter.viewport());
// getchar() // with this line the drawings update!
// les sommets
for (int i=0 ; i<3 ; i++)
this->itemDelegate()->paint(&painter, option, this->model()->index(i, 0));
}
the painting itself being delegated, as you can see.
Now I code the mouse moving event processing as below:
void
vadControlPolygon::mouseMoveEvent(QMouseEvent *e)
{
// check if we are moving any rectangle
if (!this->index.isValid())
return;
// update the model data
this->model()->setData(this->index, this->model()->data(this->index).toInt() +
(e->pos().x() - this->lastx) * 100 / this->width());
QModelIndex idx(this->index.sibling(this->index.row(), 1));
this->model()->setData(idx, this->model()->data(idx).toInt() +
(e->pos().y() - this->lasty) * 100 / this->height());
// emit that signal, which has been connected to the paintEvent() slot
emit repaint(new QPaintEvent(QRect()));
// remember the current pointer position
this->lastx = e->pos().x();
this->lasty = e->pos().y();
}
the mouse press and release event handling is not here but you can imagine what it does.
So my problem is that the painting does not change BUT if I add some kind of delay in the paintEvent() code, such as a getchar() (see the code, the getchar() is commented), the painting is updated just after the getchar() has been executed. The new figure then corresponds to the current situation, that is after all the remaining mouse move events have been processed (it means that the figure is suddenly changed, the intermediate drawings do not appear).
So could it be a problem of several events of the same type arriving in a delay too short for them to be processed? any such experience among you Qt users?
I just draw some rectangles in a painter and I want to be able to move the rectangles. The coordinates data are stored using a QAbstractItemModel.
I have a paint code as below:
void
vadControlPolygon::paintEvent(QPaintEvent *event)
{
QPainter painter(this->viewport());
painter.setWindow(0, 0, 100, 100);
QStyleOptionViewItem option;
painter.eraseRect(painter.viewport());
// getchar() // with this line the drawings update!
// les sommets
for (int i=0 ; i<3 ; i++)
this->itemDelegate()->paint(&painter, option, this->model()->index(i, 0));
}
the painting itself being delegated, as you can see.
Now I code the mouse moving event processing as below:
void
vadControlPolygon::mouseMoveEvent(QMouseEvent *e)
{
// check if we are moving any rectangle
if (!this->index.isValid())
return;
// update the model data
this->model()->setData(this->index, this->model()->data(this->index).toInt() +
(e->pos().x() - this->lastx) * 100 / this->width());
QModelIndex idx(this->index.sibling(this->index.row(), 1));
this->model()->setData(idx, this->model()->data(idx).toInt() +
(e->pos().y() - this->lasty) * 100 / this->height());
// emit that signal, which has been connected to the paintEvent() slot
emit repaint(new QPaintEvent(QRect()));
// remember the current pointer position
this->lastx = e->pos().x();
this->lasty = e->pos().y();
}
the mouse press and release event handling is not here but you can imagine what it does.
So my problem is that the painting does not change BUT if I add some kind of delay in the paintEvent() code, such as a getchar() (see the code, the getchar() is commented), the painting is updated just after the getchar() has been executed. The new figure then corresponds to the current situation, that is after all the remaining mouse move events have been processed (it means that the figure is suddenly changed, the intermediate drawings do not appear).
So could it be a problem of several events of the same type arriving in a delay too short for them to be processed? any such experience among you Qt users?