问题
I'm developing a 2D/3D animation desginer program which user can design animated items on a graphic scene, after designing user needs to be able to convert the animation into a specific video file, for rendering I just run a QTimer with Qt::PreciseTimer key (when animation is playing on the QGraphicsView) and set its interval to 33ms to get 30 shots per second and in its timeout signal I just use GraphicsView->render to get an image of what is displaying on the scene then save it to the file, the problem is that the timer timeout interval isn't accurate especially if I move window or do something else during the conversion process! even I moved the timer in to another Thread but it didn't do the trick!
Min Time: 33 Cur Time: 65 elapsed 64 Max Time: 65
Min Time: 32 Cur Time: 32 elapsed 31 Max Time: 65
Min Time: 32 Cur Time: 32 elapsed 32 Max Time: 65
Min Time: 32 Cur Time: 33 elapsed 32 Max Time: 65
Min Time: 32 Cur Time: 33 elapsed 33 Max Time: 65
Min Time: 32 Cur Time: 32 elapsed 31 Max Time: 65
Min Time: 32 Cur Time: 50 elapsed 49 Max Time: 65
Min Time: 22 Cur Time: 22 elapsed 22 Max Time: 65
Min Time: 22 Cur Time: 27 elapsed 27 Max Time: 65
Min Time: 22 Cur Time: 33 elapsed 32 Max Time: 65
Min Time: 22 Cur Time: 33 elapsed 32 Max Time: 65
Min Time: 22 Cur Time: 33 elapsed 32 Max Time: 65
Min Time: 22 Cur Time: 32 elapsed 32 Max Time: 65
Min Time: 22 Cur Time: 34 elapsed 32 Max Time: 65
for another method I used a QTimeLine which is set to the animation and by its FrameChanged singnal I supposed to find the exact end time of a frame to take picture, but again the framechange signal isn't accurate too!
Min Time: 0 Cur Time: 50 Max Time: 50
Min Time: 0 Cur Time: 32 Max Time: 50
Min Time: 0 Cur Time: 32 Max Time: 50
Min Time: 0 Cur Time: 33 Max Time: 50
Min Time: 0 Cur Time: 33 Max Time: 50
Min Time: 0 Cur Time: 33 Max Time: 50
Min Time: 0 Cur Time: 34 Max Time: 50
Min Time: 0 Cur Time: 32 Max Time: 50
Min Time: 0 Cur Time: 33 Max Time: 50
Min Time: 0 Cur Time: 25 Max Time: 50
Min Time: 0 Cur Time: 40 Max Time: 50
Min Time: 0 Cur Time: 33 Max Time: 50
Min Time: 0 Cur Time: 35 Max Time: 50
Min Time: 0 Cur Time: 31 Max Time: 50
Min Time: 0 Cur Time: 33 Max Time: 50
Min Time: 0 Cur Time: 33 Max Time: 50
Min Time: 0 Cur Time: 33 Max Time: 50
Min Time: 0 Cur Time: 33 Max Time: 50
Min Time: 0 Cur Time: 35 Max Time: 50
Min Time: 0 Cur Time: 31 Max Time: 50
Min Time: 0 Cur Time: 33 Max Time: 50
Min Time: 0 Cur Time: 32 Max Time: 50
Min Time: 0 Cur Time: 33 Max Time: 50
Min Time: 0 Cur Time: 32 Max Time: 50
Min Time: 0 Cur Time: 33 Max Time: 50
with this problem, when I have a animated graphic item which is moving linearly from left to right of the scene, after converting it to a vide file, when I paly the file I can see that item moves 3 pixels in one frame and it moves 5 pixels in another frame while it should move equal pixels in every frames!!
I'm using QElapsedTimer to fine elapsed time between every frames timeout,
what do you think is the problem ? is there any better approch to render a QGraphicsView in a video ?
and is there any other way to no showing anything on the screen when you just want to render animations into a file ? something like offscreen scene ?
some piece of codes of the program
void ZAnimator::onFrameChanged()
{
timeta = elapsedTimera->elapsed();
elapsedTimera->restart();
if (timeta > maxtimeta)
maxtimeta = timeta;
if (timeta < mintimeta)
mintimeta = timeta;
qDebug() << "Min Time:" << mintimeta <<" Cur Time: " << timeta << " Max Time:" << maxtimeta;
screenPixmap->fill(Qt::black);
m_curSB->getGV()->render(screenPainter, screenPixmap->rect(),bRect);
*tempImage = screenPixmap->toImage();
imageData.push_back(*tempImage);
}
for another solution, I moved the timer and another object with a simple slot into a new Thread, it works well when I do the things in the slot of the object in new thread but as soon as I connect the timeout signal of timer to a slot of my main GUI thread again the slot in main thread triggers late and Irregular while the slot in the object which is in new thread triggers in exact time! see
THR 32
GUI 32
THR 33
GUI 33
THR 34
GUI 34
THR 32
GUI 32
THR 33
THR 33
THR 33
THR 34
THR 32
GUI 190
GUI 0
GUI 0
GUI 0
GUI 0
THR 34
GUI 9
THR 33
来源:https://stackoverflow.com/questions/60630470/render-qgraphicsview-into-video-file-frame-time-problem