QChartView和QScatterSeries覆盖了QPointF的标签

时间:2018-08-15 10:40:55

标签: c++ qt5 qchart qchartview

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

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

1 个答案:

答案 0 :(得分:2)

为什么不更改Qt源代码就很难做到这一点

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

  2. 所有绘图均在Qt类的私有部分中完成。这是整个故事:

标签是在 QXYSeries painter->drawText(position, pointLabel);)的private part中绘制的:

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);
    }
}
ScatterChartItem paint method中调用

drawSeriesPointLabels (该类未包含在官方文档中):

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 依次在 QScatterSeries private part中创建,并且不能用自定义类替代:

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 的视口上绘制的本地画家,
    • 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);
    }
}