refachor:将自定义结构体中的QCustomPlot对象更改为QPointer,防止悬垂指针问题
This commit is contained in:
parent
323d7447ee
commit
352c0b6b39
|
|
@ -100,7 +100,8 @@ protected:
|
|||
QString dataID;
|
||||
QColor color;
|
||||
RealTimeDataType dataType;
|
||||
QCPGraph* qGraph = nullptr;
|
||||
//QCPGraph* qGraph = nullptr;
|
||||
QPointer<QCPGraph> qGraph;
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -235,13 +235,6 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
|
|||
axisCfgMap.insert(it.key(), axisConfig);
|
||||
}
|
||||
|
||||
//QHash::insert()的第二个参数接收的是副本(引起拷贝),所以采用指针可以减少拷贝从而提升效率
|
||||
QHash<RealTimeDataType, Axis*> axesMap;
|
||||
for(auto& axis : m_axes)
|
||||
{
|
||||
axesMap.insert(axis._cfg.dataType, &axis);
|
||||
}
|
||||
|
||||
//删除轴
|
||||
for(int i = 0; i < m_axes.size();)
|
||||
{
|
||||
|
|
@ -262,7 +255,7 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
|
|||
Axis& axis = m_axes[i];
|
||||
if(!axisCfgMap.contains(axis._cfg.dataType))
|
||||
{
|
||||
//先删除轴上的Graph(注意先后顺序,QCustomPlot的removeGraph会析构graph,所以先做m_graphs的删除)
|
||||
//先删除轴上的Graph(注意先后顺序,QCustomPlot的removeGraph会析构graph,所以先做m_graphs的删除,或者改为QPointer智能指针可规避此问题)
|
||||
// for(auto it = m_graphs.begin(); it != m_graphs.end();)
|
||||
// {
|
||||
// if(it.value().qGraph && it.value().qGraph->valueAxis() == axis.qAxis)
|
||||
|
|
@ -274,7 +267,7 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
|
|||
while (it.hasNext())
|
||||
{
|
||||
it.next();
|
||||
if (it.value().qGraph && it.value().qGraph->valueAxis() == axis.qAxis)
|
||||
if (it.value().qGraph.isNull() || it.value().qGraph->valueAxis() == axis.qAxis)
|
||||
it.remove(); // 直接删除当前项,无需手动管理迭代器
|
||||
}
|
||||
for(int j = 0; j < m_pCustomPlot->graphCount(); j++)
|
||||
|
|
@ -297,6 +290,12 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
|
|||
m_pCustomPlot->xAxis->setVisible(true);
|
||||
|
||||
//处理应用配置(新增或更新)
|
||||
QHash<RealTimeDataType, Axis*> axesMap; //创建map类型变量,方便后续使用.QHash::insert()的第二个参数接收的是副本(引起拷贝),所以采用指针可以减少拷贝从而提升效率
|
||||
for(auto& axis : m_axes)
|
||||
{
|
||||
axesMap.insert(axis._cfg.dataType, &axis);
|
||||
}
|
||||
|
||||
for(int i = 0; i < cfg.m_pModel_dataType->rowCount(); i++) //能保证和选取顺序一致
|
||||
//for(auto it = axisCfgMap.begin(); it != axisCfgMap.end(); ++it)
|
||||
{
|
||||
|
|
@ -356,6 +355,7 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
|
|||
axis.setQCPAxis(pAxis, false);
|
||||
}
|
||||
m_axes.append(axis);
|
||||
axesMap.insert(axis._cfg.dataType, &axis); //临时变量axesMap通过做更新
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
|
@ -365,19 +365,35 @@ void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
|
|||
arrangeAxes();
|
||||
|
||||
//2.曲线-数据源决定
|
||||
QVector<Graph> deleteGraphs;
|
||||
QList<Graph> deleteGraphs;
|
||||
for(int i = 0; i < cfg.m_pModel_dataSource->rowCount(); i++)
|
||||
{
|
||||
QString stationID = cfg.m_pModel_dataSource->item(i, 0)->data(Qt::UserRole + itemRole_stationID).toString();
|
||||
QString compoentID = cfg.m_pModel_dataSource->item(i, 0)->data(Qt::UserRole + itemRole_componentID).toString();
|
||||
QString pointID = cfg.m_pModel_dataSource->item(i, 0)->data(Qt::UserRole + itemRole_pointID).toString();
|
||||
QString graphID = stationID + "-" + compoentID + "-" + pointID;
|
||||
if(!m_graphs.contains(graphID))
|
||||
if(!m_graphs.contains(graphID)) //新增数据
|
||||
{
|
||||
RealTimeDataType dataType = (RealTimeDataType)cfg.m_pModel_dataSource->item(i, 0)->data(Qt::UserRole + itemRole_dataType).toInt();
|
||||
QCPAxis* valueAxis = axesMap.value(dataType)->qAxis;
|
||||
if(valueAxis == nullptr)
|
||||
continue;
|
||||
|
||||
Graph graph;
|
||||
graph.dataID = graphID;
|
||||
graph.dataType = dataType;
|
||||
QCPGraph* newGraph = m_pCustomPlot->addGraph(m_pCustomPlot->xAxis, valueAxis);
|
||||
if(newGraph)
|
||||
{
|
||||
QVariant colorData = cfg.m_pModel_dataSource->item(i, 0)->data(Qt::DecorationRole);
|
||||
if(colorData.isValid())
|
||||
{
|
||||
QColor color = colorData.value<QColor>();
|
||||
newGraph->setPen(QPen(color));
|
||||
}
|
||||
graph.qGraph = newGraph;
|
||||
m_graphs.insert(graphID, graph);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue