c++qtqchar

Qt chart error: ASSERT: "width > 0.0" in file painting\qrasterizer.cpp, line 761


I got an error when using QValueAxis in QtCharts:

ASSERT: "width > 0.0" in file painting\qrasterizer.cpp, line 761

This happens when the QBarSet values are all 0.

I have this example with the minimal necessary code below:

#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts>

QT_CHARTS_USE_NAMESPACE

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QChart *chart = new QChart;
    QBarSeries *series = new QBarSeries(chart);
    QBarSet *set = new QBarSet("BarSet", series);
    series->append(set);

    QValueAxis *valueAxisX = new QValueAxis(chart);

    QChartView *chartView = new QChartView(chart);

    for(int i = 0; i < 24; ++i) {
        set->append(0); //error
        //if I set like set->append(1) or anything just to make sure the values are not all 0, there will be no error.

    }
    chart->addSeries(series);
    chart->setAxisX(valueAxisX, series);

    QMainWindow window;
    window.setCentralWidget(chartView);
    window.resize(420, 300);
    window.show();

    return a.exec();
}

My program will initiate the QBarSet dynamically from a QMap. Like this:

for(auto it = map.cbegin(); it != map.cend(); ++it) {
    set->append(it.value());
}

And the QMap is initiated to something like this when the program starts:

QMap(("First", 0)("Second", 0)...)

Sometimes the QMap will not add values due to the use of the program, then all the QBarSet values will be initiated to 0. Then the program will crash because of that error.

So how can I avoid this error when the QBarSet values are all initiated to 0?


Solution

  • As the error speaks:

    ASSERT: "width > 0.0"

    You need a value greater than zero.

    Also QBarSet::append,

    void QBarSet::append(const qreal value)

    expects a qreal or double value.

    Perhaps you want to set it to 0.1 initially:

    set->append(0.1);