QtCharts - 背景、前景显示

Posted

技术标签:

【中文标题】QtCharts - 背景、前景显示【英文标题】:QtCharts - Background, foreground display 【发布时间】:2017-05-21 06:09:26 【问题描述】:

我想在我的 QChartView 中显示一个 QGraphicsRectItem。但矩形显示在图表中线条系列的后面。 例如,我尝试在 QGraphicsRectItem 上执行 setZValue(10),在 QChart 上执行 setZValue(0),但它仍然显示在后面。

显然我希望矩形中的信息显示在图表系列的前面。

构造函数

StatisticsChartView::StatisticsChartView(QWidget *parent, QChart *chart)
    : QChartView(chart, parent)


    /* Create new chart */
    _chart = new QChart();
    chart = _chart;
    _chart->setAnimationOptions(QChart::AllAnimations);

    /* Default granularity */
    m_iGranularity = DEFAULT_GRANULARITY;

    /* Creating ellipse item which will display a circle when the mouse goes over the series */
    m_ellipse = new QGraphicsEllipseItem(_chart);
    penEllipse.setColor(QColor(0, 0, 0));
    penBorder.setWidth(1);
    m_ellipse->setPen(penEllipse);

    /* Creating text item which will display the x and y value of the mouse position */
    m_coordX = new QGraphicsSimpleTextItem(_chart);
    m_coordY = new QGraphicsSimpleTextItem(_chart);
    penBorder.setColor(QColor(0, 0, 0));
    penBorder.setWidth(1);
    m_coordX->setPen(penBorder);
    m_coordY->setPen(penBorder);

    m_rectHovered = new QGraphicsRectItem(_chart);
    m_rectHovered->setBrush(QBrush(Qt::yellow));
    m_coordHoveredX = new QGraphicsSimpleTextItem(m_rectHovered);
    m_coordHoveredY = new QGraphicsSimpleTextItem(m_rectHovered);
    penBorder.setColor(QColor(0, 0, 0));
    penBorder.setWidth(1);
    m_coordHoveredX->setPen(penBorder);
    m_coordHoveredY->setPen(penBorder);

    m_lineItemX = new QGraphicsLineItem(_chart);
    m_lineItemY = new QGraphicsLineItem(_chart);
    penLine.setColor(QColor(0, 0, 0));
    penLine.setStyle(Qt::DotLine);
    m_lineItemX->setPen(penLine);
    m_lineItemY->setPen(penLine);   

    /* Enable zooming in the rectangle drawn with the left click of the mouse, zoom out with right click */
    rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
    mousePressed = 0;
    seriesHovered = false;
    setMouseTracking(true);
    _chart->setAcceptHoverEvents(true);
    _chart->setZValue(50);
    m_ellipse->setZValue(10); //so it is displayed over the series
    m_coordHoveredX->setZValue(20); //so it is displayed over others

    setRenderHint(QPainter::Antialiasing);
    setChart(_chart);


系列创作

void StatisticsChartView::drawCurve(bool bDrawScale)

    int w = WIDTH;
    int h = HEIGHT;
/* Creating series */
QLineSeries *lineFalse = new QLineSeries();
QLineSeries *lineAutomatic = new QLineSeries();
QLineSeries *lineOk = new QLineSeries();
QLineSeries *lineFalsePositive = new QLineSeries();
QLineSeries *lineManualTreatement = new QLineSeries();
QLineSeries *lineFalseNegative = new QLineSeries();

QList<QLineSeries*> lineSeriesList;
lineSeriesList << lineFalse << lineAutomatic << lineOk << lineFalsePositive << lineManualTreatement << lineFalseNegative;
QList<QString> nameSeriesList;
nameSeriesList << "False" << "Automatic" << "Ok" << "FalsePositive" << "ManualTreatement" << "FalseNegative";
QList<QVector<Graphpoint>> graphPointList;
graphPointList << gpFalse << gpDetected << gpOk << gpDetectedNotOk << gpManualTreatement << gpFalseNegative;

double graphX = 100.0 / (m_iGranularity);
bool pointsVisible = true;

for (int n = 0; n < lineSeriesList.count(); ++n)

    /* Adding points to line series */
    for (int i = 0; i < m_iGranularity + 1; ++i)
    
        lineSeriesList[n]->append(i * graphX, (float)(graphPointList[n][i]).fValue * 100);
        lineSeriesList[n]->setPointsVisible(pointsVisible);
        lineSeriesList[n]->setName(nameSeriesList[n]);
    


_chart->legend()->setVisible(true);
_chart->legend()->setAlignment(Qt::AlignBottom);

/* Setting axis X and Y */
axisX = new QValueAxis();
axisY = new QValueAxis();
axisX->setRange(0, 100);
axisY->setRange(0, 100);
/* Adding line series to the chart and attaching them to the same axis */
for (int j = 0; j < lineSeriesList.count(); ++j)

    _chart->addSeries(lineSeriesList[j]);

    _chart->setAxisX(axisX, lineSeriesList[j]);

    _chart->setAxisY(axisY, lineSeriesList[j]);

    connect(lineSeriesList[j], SIGNAL(hovered(QPointF, bool)), this, SLOT(onSeriesHovered(QPointF, bool)));



_chart->resize(w, h);

return;

在图表上绘制矩形

void StatisticsChartView::onSeriesHovered(QPointF point, bool state)

seriesHovered = state;


/* Updating the size of the rectangle */
if (mousePressed == 0 && seriesHovered == true)

    /* x and y position on the graph */
    qreal x = _chart->mapToPosition(point).x();
    qreal y = _chart->mapToPosition(point).y();

    /* x and y value on the graph from 0 to 100 for ou graph */
    qreal xVal = point.x();
    qreal yVal = point.y();

    qreal maxX = axisX->max();
    qreal minX = axisX->min();
    qreal maxY = axisY->max();
    qreal minY = axisY->min();

    /* We don't want to display value outside of the axis range */
    if (xVal <= maxX && xVal >= minX && yVal <= maxY && yVal >= minY)
    
        m_coordHoveredX->setVisible(true);
        m_coordHoveredY->setVisible(true);
        m_rectHovered->setVisible(true);
        m_ellipse->setVisible(true);

        m_rectHovered->setRect(x - 31, y - 31, 30, 30);

        qreal rectX = m_rectHovered->rect().x();
        qreal rectY = m_rectHovered->rect().y();
        qreal rectW = m_rectHovered->rect().width();
        qreal rectH = m_rectHovered->rect().height();

        /* We're setting the labels and nicely adjusting to chart axis labels (adjusting so the dot lines are centered on the label) */
        m_coordHoveredX->setPos(rectX + rectW / 4 - 3, rectY + 1);
        m_coordHoveredY->setPos(rectX + rectW / 4 - 3, rectY + rectH / 2 + 1);


        /* Setting value to displayed with four digit max, float, 1 decimal */
        m_coordHoveredX->setText(QString("%1").arg(xVal, 4, 'f', 1, '0'));
        m_coordHoveredY->setText(QString("%1").arg(yVal, 4, 'f', 1, '0'));

        m_ellipse->setRect(QRectF::QRectF(x, y, 10, 10));
        m_ellipse->setPos(x, y);
        m_ellipse->setBrush(QBrush(Qt::red));

    
    else
    
        /* We're not displaying information if out of the chart */
        m_coordHoveredX->setVisible(false);
        m_coordHoveredY->setVisible(false);
        m_rectHovered->setVisible(false);
        m_ellipse->setVisible(false);
    




else

    /* We're not displaying information if series aren't hovered */
    m_coordHoveredX->setVisible(false);
    m_coordHoveredY->setVisible(false);
    m_rectHovered->setVisible(false);
    m_ellipse->setVisible(false);


【问题讨论】:

您能否提供足够的代码来重新创建该行为? GraphPoint 是什么? 这是我自己的结构 【参考方案1】:

您应该尝试使用专门针对矩形的系列。 将其设置为图表上的最后一个系列,以高于其他线条。并为文本添加图例或标注。

【讨论】:

以上是关于QtCharts - 背景、前景显示的主要内容,如果未能解决你的问题,请参考以下文章

QtCharts setRange 问题

QtCharts模块勾画折线和曲线图

PyQt5:未安装模块“QtCharts”

技术小新 | QtCharts快速入门

技术小新 | QtCharts快速入门

Qt qml 下使QtCharts