简体   繁体   中英

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).

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 ?

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

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 the QImage onto the painter when needed.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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