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

15 篇文章 0 订阅
5 篇文章 0 订阅

搬家中。。。请关注 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值