I have a class which displays waveform data of audiofiles in a QWidget (see screenshot of the widget below, back then i sti开发者_运维技巧ll 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.
Use OpenGL and QGLWidget if you want to draw really fast.
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.
精彩评论