PDA

View Full Version : problem with making the application sleep for a sec



Wojtek.wk
2nd May 2010, 01:52
Hi! I'm trying to write the application that will:
1) in the beginning draw an ellipse in the centre of a widget
2) then draw the second ellipse and make it move
Of course this is only the part of something bigger (there will be much more ellipses), but now I have the problem to make this second ellipse move. I'm trying to remember the coordinates of the ellipse in the list and change them in the loop. And after each change of the coordinates I want the application to sleep and repaint the widget. I want to make it sleep because it's the one of the ways to make the move of the ellipse visible. I tried various methods, but unfortunately, nothing worked. I launch the application, and no move is visible - I can see only the last position of the ellipse. Could anyone help me? Thanks in advance! There is the part of my code:


void RightWindow::paintEvent(QPaintEvent* event) {
QPainter painter(this);
painter.setBrush(Qt::SolidPattern);
for(int i = 0; i < pointsList.size(); i++) {
painter.drawEllipse(QRect(pointsList.at(i), *(getRadius())));
}
}

void RightWindow::start(double x, double y) {
setNewX(x);
setNewY(y);
pointsList.append(QPoint(getNewX(), getNewY()));
repaint();
setNewX(x - 10);
setNewY(y - 10);
pointsList.append(QPoint(getNewX(), getNewY()));
repaint();
for(int i = 0; i < 10; i++) {
QPoint point = pointsList.at(pointsList.size() - 1);
point.setX(point.x() + 2);
pointsList.removeAt(pointsList.size() - 1);
pointsList.append(point);
QTimer::singleShot(1000, this, SLOT(repaint()));
repaint();
}
}


RightWindow class is my class that inherits after QWidget. And the methods such as getNewX() are my own method written by me...

Lykurg
2nd May 2010, 08:53
See QTimer (edit: ups, you already use it), QTimeLine or QPropertyAnimation.

Edit: also use update instead of repaint.
Edit2: your loop is nonesense. call the calculating function with a timer, not the update function:
void XXX::calc() {
//calculate new values
update();
// single shot to this function.
}
(a single shot is not the best solution!)