快速绘制数千个矩形 [英] Drawing thousands of rects quickly

查看:90
本文介绍了快速绘制数千个矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在QT中吸引数千个 rects (大约100,000或更多)的正确方法是什么?

What is the proper way to draw thousands of rects in QT (around 100,000 or more)?

我试过了:


  • 简单的 paintEvent() QWidget 。

  • 将对象绘制到 QImage ,然后将此图像绘制到 QWidget

  • 使用 QGraphicsScene (也许我没有正确使用它,我只是添加了 rects 到场景)

  • Simple with paintEvent() of QWidget.
  • Drawing objects to QImage then this image to QWidget.
  • Using QGraphicsScene (maybe I didn't use it properly, I just added rects to scene)

每次绘制的速度都很慢,我对此没有更多的想法如何执行此操作(也许使用opengl / directx,但这听起来不是一个好主意)。我知道存在执行此操作的应用程序,所以应该有某种方法。

Every time drawing was really slow and I don't have more ideas on how to do this (maybe with opengl/directx but this doesn't sound like a good idea). I know that there exist applications that do that so there should be some way.

编辑:

我想知道 drawRects()如何工作?有没有机会填充一些 uchar * 数组并将其传递给 QImage 会更好?

I wonder how drawRects() work? Is there a chance that filling some uchar* array and passing it to QImage will be better?

推荐答案

我发现的解决方案:

创建 uint32_t数组可以包含 widget 的所有像素,请使用 memcpy()填充。使用此数组创建 QImage 并使用 drawImage()进行显示。
可以进行一些优化,例如(对于探查器)合并继续的 rects (第二个开始时间等于第一个结束时间)。不要绘制超出时间范围的矩形。也许跳过太小的 rects
对于绘制文本之类的东西,工具提示仍然可以使用 Qt 函数。
对于最简单情况下的alpha混合,您可以只获取现有值,在循环中混合它们并编写混合值,或者为此使用 SIMD

Create array of uint32_t which can contain all pixels of widget, fill it using memcpy(). Create QImage with this array and use drawImage() to show it. It can have some optimization like (for profiler) merging rects that are continues ( start time second is equal to end of first ). Don't draw rects that are out of time bounds. Maybe skip too small rects. For drawing things like text, tool tips you can still use Qt functions. For alpha blending in simplest case you can just take existing values, blend them in loop and write blended values or maybe use SIMD for this.

当然,对于更复杂的形状,绘制起来会更困难,但我认为,它比使用 Qt 函数要快。

Of course for more complex shapes it will get harder to draw but still, I think, it will be faster than using Qt functions.

这篇关于快速绘制数千个矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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