当我使用 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...
您可以在painter中播放marker大小、图像的尺寸和绘图模式来调整矩形的大小和形状以获得一条线。
在图片中,它是黑线。如您所见,您可以对其他系列重复该过程。请记住,您不能使用 OpenGL 加速:
m_pSeries0->setUseOpenGL(true);
我的工作基于 QtCharts/QScatterSeries 示例:QScatterSeries 示例
希望它会帮助你。
弗洛里安