【发布时间】:2018-03-27 14:34:05
【问题描述】:
我有一个在任何地方都找不到的问题。我有一个 忽略 QMap.insert(Key, Value) 命令的 QMap。代码如下:
//gets the selected problem index on the ProblemList
int selProblem = ui->tree_projects->currentItem()->data(0, Qt::UserRole).toInt();
//creates a new problem, sets its values and then replaces the old one on the ProblemsList variable
ProblemSets nProblem;
if(!problemsList.isEmpty()) //problemsList is an attribute of MainWindow
nProblem = problemsList.value(selProblem);
// some data collection that has been omitted because isn't important
// temporary maps that will carry the modifications
QMap<int, QString> nResName, nResType;
//data insertion into the maps
//these are fine
nResName.insert(fIdx, results_model->data(results_model->index(fIdx, 0)).toString());
nResType.insert(fIdx, results_model->data(results_model->index(fIdx, 1)).toString());
//replaces the old maps with the new ones
nProblem.SetProbResultsNames(nResName);
nProblem.SetProbResultsTypes(nResType);
//replaces the old problem with the new one
problemsList.insert(selProblem, nProblem); //this is the line that's doing nothing
}
最后一行似乎什么也没做!我什至尝试过使用
problemsList.remove(selProblem);
problemList.insert(selProblem, nProblem);
但得到了类似的结果:地图没有插入到索引 selProblem 处。它被插入,但具有 outdated 值 - 与已删除索引相同 -。我检查了 Debug 并且所有索引和变量都是正确的,但是当 .insert 命中时,什么也没有发生。
最尴尬的是,这段代码是我用另一种方法制作的复制/粘贴,我正在使用它做类似的事情,只是改变了变量名,但那个有效。
编辑 1: 这是 nProblem、selProb 和problemsList.value(selProblem)
就在行前:
problemsList.insert(selProblem, nProblem);
selProb: 0
问题:
- ProbResultsNames:“NewRow0”
- ProbResultsType:“真实”
problemsList.value(selProblem):
- ProbResultsNames:不存在
- ProbResultsType:不存在
行后
problemsList.insert(selProblem, nProblem);
selProb: 0
问题:
- ProbResultsNames:“NewRow0”
- ProbResultsType:“真实”
problemsList.value(selProblem):
- ProbResultsNames:不存在
- ProbResultsType:不存在
编辑 2:
class ProblemSets
{
public:
ProblemSets();
virtual ~ProblemSets();
ProblemSets(const ProblemSets& other);
ProblemSets& operator=(const ProblemSets& other);
//I hid getters and setters to avoid pollution on the post
private:
int index;
bool usingBenchmark;
QString functionSelected;
QString info;
QMap<int, QString> probVars_name, probVars_type, probResultsNames, probResultsTypes;
QMap<int, float> probVars_min, probVars_max;
QMap<int, int> probVars_stpSize, probVars_stp;
int varsNumber; // holds how many vars has been created, just for display purposes
int resNumber; // holds how many results has been created, just for display purposes
};
【问题讨论】:
-
您是否尝试过先删除之前存在的任何值,然后再进行插入,以查看行为?
-
如何验证
nProblem这个值没有被插入? -
@FlorentUguet 是的!我在尝试插入之前使用了 .remove(selProblem) ;该值已按预期从 QMap 中删除,但是当我插入
nProblem时,它输入的值与删除之前的值相同。 @vahancho 我使用断点和调试来检查nProblem, selProblem和problemsList的值。我将编辑帖子以添加调试值。 -
据我了解,
problemsList是QMap<int, ProblemSets>,对吧?那么为什么问题标题指的是QMap<int, QString>?你能发布整个ProblemSets课程代码吗? -
@p-a-o-l-o 抱歉打错了!你是绝对正确的。