在 Qt Charts 的 QScatterSeries 中分别为各个点着色

问题描述 投票:0回答:0

当我们点击每个点时,我试图分别为 Qt Chart 中 QScatterSeries 的每个点着色。

//adding scatter series
for(int i = 0; i < CLUSTERSERIESCOUNT; ++i)//3 times
   {
        m_clusterseries[i] = new QScatterSeries();
        if(m_clusterseries[i])
        {
            m_clusterseries[i]->clear();
            connect(m_clusterseries[i], &QScatterSeries::clicked, this, &RadarGraphicsView::onClickedClusterScatterObject);
            connect(m_clusterseries[i], &QScatterSeries::hovered, this, &RadarGraphicsView::onHoverClusterObject);
        }
    }

for(int seriesIndex = 0; seriesIndex < CLUSTERSERIESCOUNT; ++seriesIndex)
    {
        if(seriesIndex == 0)
        {
            m_clusterseries[0]->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
            m_clusterseries[0]->setPen(Qt::SolidLine);
            m_clusterseries[0]->setBorderColor(Qt::blue);
            m_clusterseries[0]->setColor(Qt::transparent);
            m_clusterseries[0]->setName(MOVING);
            m_clusterseries[0]->setMarkerSize(7.5);

            for(int i = 0; i < m_xValuesMCluster.count(); ++i)
            {
                m_clusterseries[0]->append(QPointF(m_xValuesMCluster.at(i), m_yValuesMCluster.at(i)));
            }
        }
        else if(seriesIndex == 1)
        {
            m_clusterseries[1]->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
            m_clusterseries[1]->setPen(Qt::SolidLine);
            m_clusterseries[1]->setBorderColor(Qt::yellow);
            m_clusterseries[1]->setColor(Qt::transparent);
            m_clusterseries[1]->setName(STATIONARY);
            m_clusterseries[1]->setMarkerSize(7.5);

            for(int i = 0; i < m_xValuesSCluster.count(); ++i)
            {
                m_clusterseries[1]->append(QPointF(m_xValuesSCluster.at(i), m_yValuesSCluster.at(i)));
            }
        }
        else  if(seriesIndex == 2)
        {
            m_clusterseries[2]->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
            m_clusterseries[2]->setPen(Qt::SolidLine);
            m_clusterseries[2]->setBorderColor(Qt::gray);
            m_clusterseries[2]->setColor(Qt::transparent);
            m_clusterseries[2]->setName(UNKNOWN);
            m_clusterseries[2]->setMarkerSize(7.5);

            for(int i = 0; i < m_xValuesUNCluster.count(); ++i)
            {
                m_clusterseries[2]->append(QPointF(m_xValuesUNCluster.at(i), m_yValuesUNCluster.at(i)));
            }
        }
    }

 //add series to the chart
    for(int i = 0; i < CLUSTERSERIESCOUNT; ++i)
    {
        m_chart->addSeries(m_clusterseries[i]);
        m_clusterseries[i]->attachAxis(m_axisX);
        m_clusterseries[i]->attachAxis(m_axisY);
    }

这就是加分的方式,如下图所示。

在单击每个点时,我希望它用选定的颜色着色。

void MyView::onClickedClusterScatterObject(const QPointF& point)
{
  //color the selected cluster
  QPointF p =  m_chart->mapToPosition(point);
  QGraphicsItem *it = itemAt(mapFromScene(p));
  it->setTransformOriginPoint(it->boundingRect().center());
//the below colors the whole series
//QGraphicsColorizeEffect* effect = new QGraphicsColorizeEffect(this);
//effect->setColor(QColor(m_CRLTColor));
//it->setGraphicsEffect(effect);

 QScatterSeries* series = qobject_cast<QScatterSeries*>(sender());
 qInfo()<<__FUNCTION__<<sender()<<series->markerShape();
 if(QGraphicsRectItem *rect = qgraphicsitem_cast<QGraphicsRectItem*>(it))
{
    rect->setBrush(QColor(m_CRLTColor));
}
 m_mainScene->update();
}

使用上面的代码,我可以为选定的点着色,只有黄色系列[即m_clusterseries1].

但是,单击蓝色系列点 [即 m_clusterseries[0]],它在 qgraphicsitem_cast(it) 行中失败。 并且 rect->setBrush(color) 不执行。 为什么这样?我做错了什么?

另一个问题,收到新数据后,我清除旧系列并再次执行相同的初始化步骤。在什么都不起作用之后,即黄色系列的 graphicsitem_cast 也失败了。

QGraphicsColorizeEffect 代码,为整个系列着色,还有一些意想不到的事情,比如点击蓝色点,黄色系列开始着色。

欢迎任何为 qscatterseries 的各个点着色的建议。

qt qgraphicsitem qchart qchartview qgraphicsrectitem
© www.soinside.com 2019 - 2024. All rights reserved.