fix:修复chart图元(graph)随坐标轴动态删减自定义数据对象数据和QCustomPlot中graphs同步不一致问题

This commit is contained in:
duanshengchao 2025-08-04 15:11:35 +08:00
parent b02902a0ef
commit 338baafe74
2 changed files with 17 additions and 5 deletions

View File

@ -146,6 +146,7 @@ void dpConfigurationDialog::copyModelData(QStandardItemModel* sourceModel, QStan
{
QStandardItem* newItem = new QStandardItem(item->text());
QString strTag = item->data(Qt::UserRole + itemRole_tag).toString();
newItem->setData(strTag, Qt::UserRole + itemRole_tag);
RealTimeDataType dataType = (RealTimeDataType)item->data(Qt::UserRole + itemRole_dataType).toInt();
newItem->setData(dataType, Qt::UserRole + itemRole_dataType);
if(strTag == "point")

View File

@ -270,13 +270,15 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
if (it.value().qGraph.isNull() || it.value().qGraph->valueAxis() == axis.qAxis)
it.remove(); // 直接删除当前项,无需手动管理迭代器
}
for(int j = 0; j < m_pCustomPlot->graphCount(); j++)
for(int j = 0; j < m_pCustomPlot->graphCount();)
{
QCPGraph* graph = m_pCustomPlot->graph(j);
if(graph->valueAxis() == axis.qAxis)
{
m_pCustomPlot->removeGraph(graph);
}
else
++j;
}
//删除轴
m_pCustomPlot->axisRect()->removeAxis(axis.qAxis);
@ -355,10 +357,16 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
axis.setQCPAxis(pAxis, false);
}
m_axes.append(axis);
axesMap.insert(axis._cfg.dataType, &m_axes.last()); //临时变量axesMap通过做更新,此处不要直接使用&axis因为axis是局部变量地址会被自动释放造成堆错误
//axesMap.insert(axis._cfg.dataType, &m_axes.last()); //临时变量axesMap通过做更新,此处不要直接使用&axis因为axis是局部变量地址会被自动释放造成堆错误
#endif
}
}
//更新axesMap
axesMap.clear();
for(auto& axis : m_axes)
{
axesMap.insert(axis._cfg.dataType, &axis);
}
//重新排列坐标轴
m_axisArrangementMode = cfg.arrangement;
@ -398,7 +406,10 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
}
else //更新数据
{
m_graphs.value(graphID).synchronizeTagging = "update";
//m_graphs.value(graphID).synchronizeTagging = "update";
auto it = m_graphs.find(graphID);
if(it != m_graphs.end())
it.value().synchronizeTagging = "update";
}
}
//没有标记的就是需要删除的
@ -414,10 +425,10 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
{
Graph g = m_graphs.take(key);
if( !m_pCustomPlot->removeGraph(g.qGraph) )
qWarning() << "Failed to remove graph:" << key;
qWarning() << "Failed to remove gracloxne ph:" << key;
}
qDebug() << m_graphs.count();
qDebug() << "graph count: " << m_graphs.count() << ", " << m_pCustomPlot->graphCount();
m_updateData = true;
}