qpainter 绘画替代品(Mac 上的性能很糟糕)
我有一个类,它在 QWidget 中显示音频文件的波形数据(请参见下面小部件的屏幕截图,当时我仍然使用渐变,这导致性能不佳)。
使用多次调用 QPainter::drawLine
直接在 widget 上的 PaintEvent 中绘制音频数据(对 QWidget::drawLine
的最小调用量相当于宽度小部件的 => 每个 x 坐标至少一行)。 虽然该方法在 Windows 上运行得很好(全屏下的 PaintEvent 大约需要 4 毫秒),但当程序在 MacOS 下运行时,性能会差 4-5 倍。
绘画的性能对于显示数据的流畅滚动非常重要。
所以我的问题是,有谁知道 QPainter.drawLine 更快的替代方案来绘制线条(平台相关的解决方案可能没问题,只要它们可以在 PaintEvent 中使用),或者有没有一种方法可以加速滚动,某种缓冲等?
I have a class which displays waveform data of audiofiles in a QWidget (see screenshot of the widget below, back then i still used a gradient, which caused poor performance).
The audio data is painted in the paintEvent directly on the widget using multiple calls to QPainter::drawLine
(the minimum amount of calls to QWidget::drawLine
is equivalent to the width of the widget => at least one line for each x coordinate).
While the approach works quite well on Windows (a paintEvent in fullscreen takes around ~4ms), the performance is 4-5 times worse when the program is run under MacOS.
The performance of the painting is important for fluid scrolling of the displayed data.
So my question is, does anyone know a faster alternative to QPainter.drawLine to paint lines (platform dependant solutions might be ok, as long as they can be used in a paintEvent), or is there a way to speed up scrolling, some kind of buffering etc ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Qt 的当前版本(4.7.x)使用 Core Graphics 后端进行绘画。正如您所发现的,有时它可能会很慢。在 Windows 上,它使用具有非常好的性能的软件渲染器。
我的建议是不要在绘画事件中直接在通过的画家上绘画。相反,创建一个与小部件绘制区域大小相同的
QImage
并在其上绘制。这将使用速度更快的软件渲染器。然后在需要时将QImage
绘制到画家上。The current version (4.7.x) of Qt uses Core Graphics backend for painting. It can be slow at times as you found out. On Windows, it uses a software renderer which has really good performances.
My suggestion is to not paint on the passed painter directly in your paint event. Instead, create a
QImage
the same size as your widget paint area and paint on it. This will use the software renderer which is much faster. Then plot theQImage
onto the painter when needed.如果您想快速绘制,请使用 OpenGL 和 QGLWidget。
Use OpenGL and QGLWidget if you want to draw really fast.
您可以构造一个 QPainterPath 并绘制它,而不是重复调用 drawLine 函数。而且,您可以缓存路径,因此第一次绘制后速度会快得多。
You could construct a QPainterPath and paint that instead of calling the drawLine function repeatedly. And also, you could cache the path, so it would be much faster after the first paint.