Qt - 调整大小时避免白色背景(立即设置背景颜色) [英] Qt - avoid white background when resizing (set immediately a background color)

查看:65
本文介绍了Qt - 调整大小时避免白色背景(立即设置背景颜色)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题最后以粗体显示,因此请整体阅读.

The question is in bold at the end so please read this as a whole.

我有一个 QAbstractScrollArea 小部件,我在 OnPaint() 事件中手动并完全渲染.

I have a QAbstractScrollArea widget that I manually and completely render in the OnPaint() event.

在它的构造函数中我设置了

In its constructor I set

setAttribute( Qt::WA_OpaquePaintEvent, true );
setAttribute( Qt::WA_NoSystemBackground, true );
setStyleSheet( "QWidget { background-color: rgb(0,0,77); }" );

绘制事件如下所示:

void MyArea::paintEvent (QPaintEvent *event) {
  QPainter view(viewport());
  view.fillRect(rect(), backgroundBrush);

  renderedPixmap = heavyAndSlowRenderingOnAPixmap();

  view.drawPixmap(viewRect, renderedPixmap, pixmapRect);
}

如您所见,将内容渲染到像素图中涉及一个慢"函数.

as you can see there's a "slow" function involved to render stuff into a pixmap.

问题是:当我调整窗口大小时,我可以看到在尚未重新绘制的区域中有片刻的白色闪烁

The problem is: when I resize the window, I can see for a moment a white flickering in the area not yet redrawn

我知道在重新绘制视口之前我无法避开白色区域,但是我想立即用背景颜色绘制该白色区域.

I know I can't avoid the white region until the viewport has been redrawn, but I'd like to draw that white region with the background color immediately.

那么问题来了:我可以在重像素图渲染之前立即显示背景颜色吗?

我似乎无法找到一种方法来实现这一点..似乎所有图形操作都被缓冲,然后立即位比特传输到屏幕上.我使用的是 Windows 8.1 x64 和 Qt5.有什么方法可以立即绘制背景颜色,然后继续渲染的其余部分?

I can't seem to find a way to achieve this.. it seems all graphics operations are buffered and then immediately bit-blitted to the screen together. I'm using Windows 8.1 x64 and Qt5. Any way to immediately draw the background color and then proceed with the rest of the rendering?

推荐答案

最好的解决方案是将昂贵的渲染从paintEvent() 中移出,可能转移到另一个线程中.无论如何,您都希望缓存渲染的像素图.在真正需要时更新该像素图,然后调用 update() 以触发重绘.

The best solution would be to move the expensive rendering out of the paintEvent(), potentially into another thread. You'll want to cache the rendered pixmap anyway. Update that pixmap when really needed and then call update() to trigger a repaint.

这篇关于Qt - 调整大小时避免白色背景(立即设置背景颜色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆