Qt World Summit: Submit your Presentation

Speed Problem with Qchart and Drawing BarChart in Qt5.7

  • Hello to All
    I have serious problem with drawing Chart in Qt, I created the following simple project base on Qt Bar chart Sample project, My chart changes over time dynamically. My problem with it is, when I increase the number of bars the program is too slow and it takes a long time to drawing chart. I use a thread but it does not matter at all. So how can I increase the speed of drawing chart?

    #define BinNumber 100
    #define BinLenght   0.002
    unsigned long hist[BinNumber];
    int main(int argc, char *argv[])
        QApplication a(argc, argv);
            QMainWindow window;
            QList<QBarSet*> Bins;
            QBarSeries *series;
            QChart *chart;
            QChartView *chartView;
            float deltaV = BinLenght / float(BinNumber);
            series = new QBarSeries();
            chart = new QChart();
            clock_t chartStart = clock();
            for (int i = 0; i < BinNumber; i++) {
                hist[i] = (int)qrand() % 1000;
                QBarSet * bar = new QBarSet(QString::number((i + 1)*deltaV));
                *bar << hist[i];
            while (false)
                int i = 0;
                hist[i] = 0;
                QBarSet * bar = new QBarSet(QString::number((i + 1)*deltaV));
                *bar << 0;
            QValueAxis *axisX = new QValueAxis;
            axisX->setRange(0, 5);
            QValueAxis *axisY = new QValueAxis;
            axisY->setRange(0, 1000);
            chart->setTitle("Histogram Of Input Signal");
            chart->setAxisY(axisY, series);
            chartView = new QChartView(chart);
            window.resize(420, 300);
            clock_t chartend = clock();
            qDebug()<< "Execution time is :" << (float(chartend - chartStart) /CLOCKS_PER_SEC);
        return a.exec();

    I should mention that I measure my code with some powerful profiler and the command “chart->addSeries(series);” take 510 millisecond to execute my forloop take 3039 ms for 100 BinNumber. QChart does any method for fast drawing? How can I edit this code for increasing drawing speed?

  • @hadi_m
    I think maybe the new statement in the for loop dominate the speed.

    you have already knows how many bars you need, why not use a QList or QVector to allocate the memory first, and then go though it and set the proper information to the elements?

  • Thanks for your replying
    I tested with local QBarSet Qlist instead of dynamically created it but it doesn’t matter on speed.
    I should mention I test my program by some powerful performance validator tools and I notice the line “chart->addSeries(series);” take too long time and of course my forloop take time when BarNumber Increases to 1000.

  • @hadi_m bar set is slow。unfortunately,no way resolve it

  • @hadi_m
    Try by taking different thread for those two loops and addSeries can do it in main thread only.

  • Lifetime Qt Champion

    @Venkatesh-V If chart->addSeries(series); is the problem, then I don't think using multi threading will give any advantage (instead it will complicate stuff).

Log in to reply