gpt4 book ai didi

c++ - QChartView 和 QScatterSeries 覆盖了 QPointF 的标签

转载 作者:搜寻专家 更新时间:2023-10-31 02:05:31 25 4
gpt4 key购买 nike

我有一个 QChartView,它显示一些 2D 点,每个点代表一个特定项目 我想用项目名称标记每个点,而不是用它的 x、y 坐标作为默认行为 enter image description here

有什么方法可以实现覆盖创建或渲染标签的功能?

最佳答案

为什么在不更改 Qt 源代码的情况下很难实现这一点

  1. QXYSeries::setPointLabelsFormat对你帮助不大。它确实允许您更改标签的格式,但它唯一可变的部分是点的坐标。

  2. 所有的绘图都是在Qt类的私有(private)部分完成的。这是整个故事:

标签绘制在 private partQXYSeries (painter->drawText(position, pointLabel);):

void QXYSeriesPrivate::drawSeriesPointLabels(QPainter *painter, const QVector<QPointF> &points,
const int offset)
{
if (points.size() == 0)
return;
static const QString xPointTag(QLatin1String("@xPoint"));
static const QString yPointTag(QLatin1String("@yPoint"));
const int labelOffset = offset + 2;
painter->setFont(m_pointLabelsFont);
painter->setPen(QPen(m_pointLabelsColor));
QFontMetrics fm(painter->font());
// m_points is used for the label here as it has the series point information
// points variable passed is used for positioning because it has the coordinates
const int pointCount = qMin(points.size(), m_points.size());
for (int i(0); i < pointCount; i++) {
QString pointLabel = m_pointLabelsFormat;
pointLabel.replace(xPointTag, presenter()->numberToString(m_points.at(i).x()));
pointLabel.replace(yPointTag, presenter()->numberToString(m_points.at(i).y()));
// Position text in relation to the point
int pointLabelWidth = fm.width(pointLabel);
QPointF position(points.at(i));
position.setX(position.x() - pointLabelWidth / 2);
position.setY(position.y() - labelOffset);
painter->drawText(position, pointLabel);
}
}

drawSeriesPointLabelspaint method 调用ScatterChartItem(该类未包含在官方文档中):

void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
if (m_series->useOpenGL())
return;
QRectF clipRect = QRectF(QPointF(0, 0), domain()->size());
painter->save();
painter->setClipRect(clipRect);
if (m_pointLabelsVisible) {
if (m_pointLabelsClipping)
painter->setClipping(true);
else
painter->setClipping(false);
m_series->d_func()->drawSeriesPointLabels(painter, m_points,
m_series->markerSize() / 2
+ m_series->pen().width());
}
painter->restore();
}

ScatterChartItem 又是在 private part 中创建的QScatterSeries 并且不能用自定义类替换:

void QScatterSeriesPrivate::initializeGraphics(QGraphicsItem* parent)
{
Q_Q(QScatterSeries);
ScatterChartItem *scatter = new ScatterChartItem(q,parent);
m_item.reset(scatter);
QAbstractSeriesPrivate::initializeGraphics(parent);
}

你可能想尝试什么

  1. setPointLabelsVisible(false);隐藏原来的标签,之后标签会单独绘制。

  2. 子类 QChartView 并重新实现 paintEvent,首先调用 QChartView::paintEvent 然后调用自定义函数(比如说 drawCustomLabels),它是 QXYSeriesPrivate::drawSeriesPointLabels 的修改版本。通过调用 drawCustomLabels 传递:

    • 本地画家在 MyChartView 的视口(viewport)上作画,
    • QXYSeries::points 返回的点,
    • 所需的偏移量。

下面是 drawCustomLabels 的示例:

void MyChartView::drawCustomLabels(QPainter *painter, const QVector<QPointF> &points, const int offset)
{
if (points.count() == 0)
return;

QFontMetrics fm(painter->font());
const int labelOffset = offset + 2;

painter->setFont(m_pointLabelsFont); // Use QXYSeries::pointLabelsFont() to access m_pointLabelsFont
painter->setPen(QPen(m_pointLabelsColor)); // Use QXYSeries::pointLabelsColor() to access m_pointLabelsColor

for (int n(0); n < points.count(); n++) {
QString pointLabel = "..."; // Set the desired label for the n-th point of the series
// Position text in relation to the point
int pointLabelWidth = fm.width(pointLabel);
QPointF position(points.at(n));
position.setX(position.x() - pointLabelWidth / 2);
position.setY(position.y() - labelOffset);
painter->drawText(position, pointLabel);
}
}

关于c++ - QChartView 和 QScatterSeries 覆盖了 QPointF 的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51857201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com