简体   繁体   English

qpainter绘画替代品(性能在Mac上很糟糕)

[英]qpainter painting alternatives (performance sucks on Mac)

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). 我有一个类在QWidget中显示音频文件的波形数据(见下面小部件的屏幕截图,然后我仍然使用渐变,这导致性能不佳)。

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). 使用对QPainter::drawLine多次调用,直接在小部件上的paintEvent中绘制音频数据(对QWidget::drawLine的最小调用量相当于小部件的宽度=>每个x坐标至少有一行) 。 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. 虽然该方法在Windows上运行良好(全屏的paintEvent大约需要4毫秒),但在MacOS下运行程序时,性能会下降4-5倍。

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 ? 所以我的问题是,有没有人知道更快的QPainter.drawLine替代方法来绘制线条(平台相关的解决方案可能没问题,只要它们可以在paintEvent中使用),或者有没有办法加快滚动,某种缓冲等?

小部件的旧屏幕截图(仍然使用渐变,导致性能不佳)

The current version (4.7.x) of Qt uses Core Graphics backend for painting. Qt的当前版本(4.7.x)使用​​Core Graphics后端进行绘制。 It can be slow at times as you found out. 你发现它有时很慢。 On Windows, it uses a software renderer which has really good performances. 在Windows上,它使用的软件渲染器具有非常好的性能。

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. 相反,创建一个与小部件绘制区域大小相同的QImage并在其上绘制。 This will use the software renderer which is much faster. 这将使用更快的软件渲染器。 Then plot the QImage onto the painter when needed. 然后在需要时将QImage绘制到画家QImage

如果你想快速绘制,请使用OpenGL和QGLWidget

You could construct a QPainterPath and paint that instead of calling the drawLine function repeatedly. 您可以构造一个QPainterPath并绘制它,而不是重复调用drawLine函数。 And also, you could cache the path, so it would be much faster after the first paint. 而且,您可以缓存路径,因此在第一次绘制后会更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM