Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem with QPen usage with QAreaChart



  • Hello everybody, I am using QAreaChart in order to create different rectangles, how ever the Pen used when drawing tends to exist until the X-Axis, not around the rectangle, like this image
    4af03930-9dc8-4db0-9c8e-eed087b4c605-image.png

    here's the simple code

    #include <QtWidgets/QApplication>
    #include <QtWidgets/QMainWindow>
    #include <QtCharts/QChartView>
    #include <QtCharts/QLineSeries>
    #include <QtCharts/QAreaSeries>
    
    QT_CHARTS_USE_NAMESPACE
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
    //![1]
        QLineSeries *series0 = new QLineSeries();
        QLineSeries *series1 = new QLineSeries();
    //![1]
    
    //![2]
        *series0 << QPointF(1, 10) << QPointF(5, 10) << QPointF(5, 5) << QPointF(1, 5);
    
    //![2]
    
    //![3]
        QAreaSeries *series = new QAreaSeries(series0);
    
        QPen pen(0x059605);
        pen.setWidth(3);
        series->setPen(pen);
    
        QLinearGradient gradient(QPointF(0, 0), QPointF(0, 1));
        gradient.setColorAt(0.0, 0x3cc63c);
        gradient.setColorAt(1.0, 0x26f626);
        gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
        series->setBrush(gradient);
    //![3]
    
    //![4]
        QChart *chart = new QChart();
        chart->addSeries(series);
        chart->setTitle("Simple areachart example");
        chart->createDefaultAxes();
        chart->axes(Qt::Horizontal).first()->setRange(0, 20);
        chart->axes(Qt::Vertical).first()->setRange(0, 20);
    //![4]
    
    //![5]
        QChartView *chartView = new QChartView(chart);
        chartView->setRenderHint(QPainter::Antialiasing);
    //![5]
    
    //![6]
        QMainWindow window;
        window.setCentralWidget(chartView);
        window.resize(400, 300);
        window.show();
    //![6]
    
        return a.exec();
    }
    
    

    any idea how to fix this? Thanks.


  • Lifetime Qt Champion

    Hi
    The docs says
    "To create area charts, we need two QLineSeries instances. They are going to define the upper and lower boundary of the area."

    And you seem to only use one.
    QAreaSeries *series = new QAreaSeries(series0, MISSING!);

    I think you have to define the rectangle as 2 series. like top and bottom.
    I dont have QCharts installed and the installer is acting up so i cant give more details.



  • I split it into two, like this

        *series0 << QPointF(1, 10) << QPointF(5, 10);
            *series1 <<QPointF(5, 5) << QPointF(1, 5);
    //![2]
    
    //![3]
        QAreaSeries *series = new QAreaSeries(series0,series1);
    

    but now the shape is like this
    cd36ceed-245d-43f4-b005-b05b1fc7b116-image.png
    any idea why or how to fix?

    Thanks in advance.


Log in to reply