`

[转载]解决 Qt / QML 中图像的闪烁 (Avoiding graphics flicker in Qt / QML )

QT 
阅读更多

搬家中。。。请关注 nuihq.com

转载自:http://blog.rburchell.com/2011/11/avoiding-graphics-flicker-in-qt-qml.html

It’s very common when writing QML applications to write a small stub, something like the following:

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.

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.

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.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.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics