QCustomPlot Discussion and Comments

Performance of plot degrades massively when increasing pen widthReturn to overview

I have a simple example app here which creates some random data (50 samples) and draws a line plot which has dragging enabled.

I have a single constant, PEN_WIDTH, which I change from a value of 1 to 3:

const int PEN_WIDTH = 3;

This constant is used in the call to QCPGraph::setPen

        QCPGraph* graph = plot.addGraph();
        graph->setPen(QPen(Qt::red, PEN_WIDTH));

When using a PEN_WIDTH of 1, dragging is near instantaneous.
When using a PEN_WIDTH of 3, dragging lags drastically, like several seconds.


How can I have a larger pen width without sacrificing performance?

Full example below:

#include <QApplication>
#include <QMainWindow>
#include <qcustomplot/qcustomplot.h>
#include <random>

const int PEN_WIDTH   = 3;
const int NUM_SAMPLES = 50;

class Plot
    Plot(int& argc, char** argv)
        : app(argc, argv)

        plot.axisRect()->setRangeDrag(Qt::Horizontal | Qt::Vertical);


    int exec()
        std::random_device rd;
        std::mt19937 mt(rd());
        std::uniform_real_distribution<double> dist(0, 10);

        QVector<double> x,y;
        double i = 0;
        while (i < NUM_SAMPLES)
            y.push_back(i * dist(mt));
            i += .1;

        QCPGraph* graph = plot.addGraph();
        graph->setPen(QPen(Qt::red, PEN_WIDTH));
        graph->addData(x, y);


        return app.exec();
    QApplication app;
    QMainWindow  window;
    QCustomPlot  plot;

int main(int argc, char** argv)
    return Plot(argc, argv).exec();

In case it has some bearing on the cause/solution, I am running this on Ubuntu 14.04 with Gnome 3 desktop. I am using onboard Xeon E3 graphics

$ lspci -v -s 00:02.0
00:02.0 VGA compatible controller: Intel Corporation Xeon E3-1200 v3/4th Gen Core Processor Integrated Graphics Controller (rev 06) (prog-if 00 [VGA controller])
	Subsystem: Hewlett-Packard Company Device 1905
	Flags: bus master, fast devsel, latency 0, IRQ 45
	Memory at ef400000 (64-bit, non-prefetchable) [size=4M]
	Memory at c0000000 (64-bit, prefetchable) [size=256M]
	I/O ports at f000 [size=64]
	Expansion ROM at <unassigned> [disabled]
	Capabilities: <access denied>
	Kernel driver in use: i915

Hi Steve,

This is a known property of the Qt raster painting system, that pen widths > 1 are significantly more complex to draw and thus take more time (basically it turns a simple line into a complex polygon triangulation). That's why for performance critical situations (e.g. when having lots of points) I recommend using only 1 pixel wide lines or having hardware which is capable of achieving the wanted replot rate (see performance improvement hints in the documentation: http://www.qcustomplot.com/documentation/performanceimprovement.html).

However there's good news, too: QCP 2.0 will (re-)introduce a very simple way to turn on OpenGL hardware acceleration. It will be as simple as something like "plot->setOpenGl(True)", and will give a significant performance boost for complex shapes -- including thick lines. QCP2.0 beta is currently in development, I'm reluctant to give a release date though because then people rely on it and it's a big release which has already (and might again) bring up unexpected delays.

Thanks DerManu

Is there anything I can do in the mean time to improve performance whilst still having a pen width > 1?

You mention OpenGL support coming. However, I see in the article you linked there is mention of QApplication::setGraphicsSystem("opengl") (Only available for Qt versions before 5.0) I'm using Qt 5.5

Was OpenGL support removed? Is there a way I can enable OpenGL now?


So really the answer is - I can't use thicker pens?

The Qt developers have decided for Qt5.0 that OpenGL support needs to be refactored substantially. In the course of this the previous mechanisms to enable hardware accelerated painting were removed, as mentioned above. This of course also affected QCustomPlot.

So yes, right now if you're using Qt >= 5.0 and experience performance issues on your target platform due to thick pen widths, avoid thick pen widths. You can make them thicker again once QCP2 is out and has restored OGL :). In case you're a commercial customer, please contact me via the support email.