Qt5 QtChart 在使用 QScatterSeries 时删除垂直线

Qt5 QtChart drop vertical lines while using QScatterSeries

当我使用QScatterSeries 时,我可以非常轻松地在(x, y) 处绘制点。但是,我想画短线而不是点,如下图所示。我怎样才能这样做?

我尝试使用 RectangleMarker,但它只能绘制一个粗正方形。我更喜欢大约 2px 宽和 20px 高的细线。

有没有办法添加自定义标记形状?

这是我用来将我的点转换为线的代码和设置:

//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...

可以在画笔中播放marker的大小,图片的尺寸和绘图图案,适配矩形的大小和形状,得到一条线。

图中,就是黑线。如您所见,您可以对其他系列重复该过程。 请记住,您不能使用 openGL 加速:

m_pSeries0->setUseOpenGL(true);

我的工作基于 QtCharts/QScatterSeries 示例:QScatterSeries example

希望对您有所帮助。

弗洛里安