QtCharts-背景,前景显示

问题描述 投票:3回答:1

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

enter image description here

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

构造函数

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);
}
}
c++ qt charts qgraphicsitem qtcharts
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.