Search code examples
qt5qtcharts

Qt5 QtChart drop vertical lines while using QScatterSeries


When I am using QScatterSeries, I can very easily draw point at (x, y). However, instead of points I would like to draw short lines, like in the figure below.enter image description here How can I get about doing so?

I tried using RectangleMarker, but it just draws a fat square. I would prefer a thin line about 2px wide and 20px in height.

Is there a way I can add custom marker shapes?


Solution

  • Here are the code and the settings I use to transform my points into lines :

    //create scatter series to draw point
    m_pSeries1 = new QtCharts::QScatterSeries();
    m_pSeries1->setName("trig");
    m_pSeries1->setMarkerSize(100.0);
    
    //draw a thin rectangle (50 to 50)
    QPainterPath linePath;
    linePath.moveTo(50, 0);
    linePath.lineTo(50, 100);
    linePath.closeSubpath();
    
    //adapt the size of the image with the size of your rectangle
    QImage line1(100, 100, QImage::Format_ARGB32); 
    line1.fill(Qt::transparent);
    QPainter painter1(&line1);
    painter1.setRenderHint(QPainter::Antialiasing);
    painter1.setPen(QColor(0, 0, 0));
    painter1.setBrush(painter1.pen().color());
    painter1.drawPath(linePath);
    
    //attach your image of rectangle to your series
    m_pSeries1->setBrush(line1);
    m_pSeries1->setPen(QColor(Qt::transparent));
    
    //then use the classic QtChart pipeline...
    

    You can play the marker size, the dimension of the image and the drawing pattern in the painter to adapt the size and shape of the rectangle to obtain a line.

    enter image description here

    In the picture, it's the black line. As you can see you can repeat the process for other series. Keep in mind that you cannot use the openGL acceleration:

    m_pSeries0->setUseOpenGL(true);
    

    My work is based on the QtCharts/QScatterSeries example : QScatterSeries example

    Hope it will help you.

    Florian