I expect to see a smooth movement, but instead there is a 10 second delay then suddenly the whole movement occurs.
Because this for loop does not let control return to the Qt event loop, which needs to process the translation and repaint the window for each move. Qt's event loop removes all but the last paint event when the loop contains multiple pending paint requests.
for (int i = 0; i<1000; i++)
{
mover(delta);
}
To avoid this mistake, get rid of the loop and the sleep command in runMover(). Instead, set up a QTimer with a 10 ms timeout. Connect the timer's timeout() signal to a slot that translates the objects by one step, checks to see how many steps have been made, and starts the QTimer again if there are less than 10 second's worth of steps (1000).
void MainWindow::runMover()
{
// elapsedSteps is a member variable of MainWindow that is inotialized to 0 before
// calling runMover()
if ( elapsedSteps < 1000 )
{
QVector3D delta = QVector3D(0.0f, 0.0f, -0.1f);
mover( delta );
QTimer::singleShot( 10,
this,
&MainWindow
::runMover );
elapsedSteps++;
}
}
void MainWindow::runMover()
{
// elapsedSteps is a member variable of MainWindow that is inotialized to 0 before
// calling runMover()
if ( elapsedSteps < 1000 )
{
QVector3D delta = QVector3D(0.0f, 0.0f, -0.1f);
mover( delta );
QTimer::singleShot( 10, this, &MainWindow::runMover );
elapsedSteps++;
}
}
To copy to clipboard, switch view to plain text mode
Please use CODE tags when posting source code so it is readable. See my signature below.
Bookmarks