c++ - QtCharts——后台、前台显示

标签 c++ qt charts qgraphicsitem qtcharts

我想在我的 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++ - QtCharts——后台、前台显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41506384/

相关文章:

c++ - Visual 2010 中带有 lambda 函数和枚举的 C2665,是错误还是正常?

c++ - 从函数生成一个 char* 指针数组

c++ - 如何在 Qt Creator 项目向导中添加自定义构建步骤?

ios - Shinobi Charts SChartCategoryAxis 和 SChartLineSeries 切入中间最后一个刻度标记标签

python - 用图形可视化带有字符串的 numpy bool 数组

c++ - 推送到作为函数参数传递的队列

c++ - VERIFY(...) 是 C++ 编码的好习惯吗?

c++ - Phonon 不渲染视频

c++ - Qt调用外部Python脚本

ios - 如何在 MPAndroidChart 中缩小时隐藏值标签?