QChart 选中图例对应折线变色

因为项目中需要用的折线比较多,查找某一条比较困难所以做了选中图例折线变色的功能,代码比较简单:

QColor              m_color;

QChart                               m_chart;

void MainWindow::markers()//关联初始化
{
    foreach(QLegendMarker* pLegendMarker, m_chart->legend()->markers())
    {
        connect(pLegendMarker, SIGNAL(hovered(bool)), this,SLOT(sltHandleMarkerClicked(bool)));
    }
}

 

void MainWindow::sltHandleMarkerClicked(bool status)
{
    QLegendMarker* poLegendMarker = qobject_cast (sender());
    switch (poLegendMarker->type())
    {
    case QLegendMarker::LegendMarkerTypeXY:
    {
        QLineSeries* LineSeries = dynamic_cast(poLegendMarker->series());
        poLegendMarker->setVisible(true);
        if(status)
        {
            m_color = LineSeries->color();
            LineSeries->setColor(QColor(255,0,0));
        }
        else{
            LineSeries->setColor(m_color);
            LineSeries->chart()->legend()->setColor(m_color);
        }
        break;
    }
    default:
    {
        qDebug() << "Unknown type";
        break;
    }
    }
}

效果图:前

QChart 选中图例对应折线变色_第1张图片

后:

QChart 选中图例对应折线变色_第2张图片

下载:https://download.csdn.net/download/u010304326/10930311

你可能感兴趣的:(Qt,QChart,QLegend)