点击QChart图例,显示或隐藏 对应的折线
原文链接:C++ Qt开发:Charts折线图绑定事件-腾讯云开发者社区-腾讯云
// 设置图表的主题 m_chart->setTheme(QChart::ChartThemeBlueNcs); QLegend *legend = m_chart->legend(); legend->setAlignment(Qt::AlignTop); // 设置图例的位置为底部 legend->setVisible(true); // 显示图例 legend->setBorderColor(Qt::black); // 设置图例边框颜色 // 显示或隐藏图例对应的折线 foreach (QLegendMarker *marker, legend->markers()) { connect(marker,&QLegendMarker::clicked,this,[&](){ qDebug() <<"点击图例:"; // 将发送者强制转换为 QLegendMarker 类型 QLegendMarker* marker = qobject_cast<QLegendMarker*>(sender()); // 检查标记的类型 switch (marker->type()) { case QLegendMarker::LegendMarkerTypeXY: { // 切换数据系列的可见性 marker->series()->setVisible(!marker->series()->isVisible()); // 设置标记可见 marker->setVisible(true); // 根据数据系列的可见性设置标记的透明度 qreal alpha = 1.0; if (!marker->series()->isVisible()) alpha = 0.5; // 调整标记的标签刷颜色透明度 QColor color; QBrush brush = marker->labelBrush(); color = brush.color(); color.setAlphaF(alpha); brush.setColor(color); marker->setLabelBrush(brush); // 调整标记的刷颜色透明度 brush = marker->brush(); color = brush.color(); color.setAlphaF(alpha); brush.setColor(color); marker->setBrush(brush); // 调整标记的画笔颜色透明度 QPen pen = marker->pen(); color = pen.color(); color.setAlphaF(alpha); pen.setColor(color); marker->setPen(pen); break; } default: break; } }); }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验
2024-01-01 qt QPainter学习 窗口毛玻璃效果,模糊背景