Avoiding graphics flicker in Qt / QML
int main(int argc, char **argv)
{
QApplication application(argc, argv);
QDeclarativeView view;
view.setSource(QUrl("qrc:/qml/main.qml"));
view.showFullScreen();
return a.exec();
}
What's wrong with this? It's a very subtle problem. I'll give you a moment to think about it, and a video to see if you notice the problem. Make sure you don't cheat.
(demonstrating removal of flicker in QML)
Back already? Have you figured it out? That's right, it flickers. Horrifically.
So what causes this? By default, QWidgets are drawn parent first, with parents drawing children. When a widget is drawn, first, it draws its background, then it draws the actual content. That background proves to be a problem, in this case.
If we add the following lines to the above example, the flicker goes away, and my eyes no longer want to bleed:
view.setAttribute(Qt::WA_OpaquePaintEvent);
view.setAttribute(Qt::WA_NoSystemBackground);
view.viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
view.viewport()->setAttribute(Qt::WA_NoSystemBackground);
NB: I'm not completely sure that adding it to both the view, and the viewport is completely necessary, but it can't harm at least. Make sure to re-set it if you change viewports.
view.setAttribute(Qt::WA_NoSystemBackground);
view.viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
view.viewport()->setAttribute(Qt::WA_NoSystemBackground);
NB: I'm not completely sure that adding it to both the view, and the viewport is completely necessary, but it can't harm at least. Make sure to re-set it if you change viewports.
For completeness, here's the full, fixed example:
int main(int argc, char **argv)
{
QApplication application(argc, argv);
QDeclarativeView view;
view.setSource(QUrl("qrc:/qml/main.qml"));
view.setAttribute(Qt::WA_OpaquePaintEvent);
view.showFullScreen();
return a.exec();
}
(If you're curious, Qt::WA_OpaquePaintEvent basically implies that you'll repaint everything as necessary yourself (which QML is well behaved with), and Qt::WA_NoSystemBackground tells Qt to nicely not paint the background.)
NB: on Harmattan (and Nemo Mobile) at least, make sure you always use QWidget::showFullScreen(). The compositor in use there unredirects fullscreen windows (meaning no compositor in the way), so you get faster drawing performance, and every frame counts.
(obligatory thanks to Daniel Stone of X and Collabora fame, for telling me to stop blaming X, and start blaming the crappy toolkits ☺)
{
QApplication application(argc, argv);
QDeclarativeView view;
view.setSource(QUrl("qrc:/qml/main.qml"));
view.setAttribute(Qt::WA_OpaquePaintEvent);
view.setAttribute(Qt::WA_NoSystemBackground);
view.viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
view.viewport()->setAttribute(Qt::WA_NoSystemBackground);
view.viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
view.viewport()->setAttribute(Qt::WA_NoSystemBackground);
view.showFullScreen();
return a.exec();
}
(If you're curious, Qt::WA_OpaquePaintEvent basically implies that you'll repaint everything as necessary yourself (which QML is well behaved with), and Qt::WA_NoSystemBackground tells Qt to nicely not paint the background.)
NB: on Harmattan (and Nemo Mobile) at least, make sure you always use QWidget::showFullScreen(). The compositor in use there unredirects fullscreen windows (meaning no compositor in the way), so you get faster drawing performance, and every frame counts.
(obligatory thanks to Daniel Stone of X and Collabora fame, for telling me to stop blaming X, and start blaming the crappy toolkits ☺)