Qt QMap<int, MyClass> 忽略插入命令
Qt QMap<int, MyClass> ignores insert command
我有一个在任何地方都找不到的问题。我有一个 QMap 忽略 QMap.insert(Key, Value) 命令。这是代码:
//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 值 - 与已删除索引相同的值 -。我检查了调试,所有索引和变量都正确,但是当 .insert 命中时,没有任何反应。
最尴尬的是这段代码是 copy/paste 我用我正在使用的另一种方法制作的,它做类似的事情,只是改变变量名,但那个有效。
编辑 1: 这是 nProblem, 的内容selProb 和 problemsList.value(selProblem)
行前:
problemsList.insert(selProblem, nProblem);
selProb: 0
n问题:
- 概率结果名称:"NewRow0"
- 概率结果类型:"Real"
problemsList.value(selProblem):
- ProbResultsNames:不存在
- 概率结果类型:不存在
行后
problemsList.insert(selProblem, nProblem);
selProb: 0
n问题:
- 概率结果名称:"NewRow0"
- 概率结果类型:"Real"
problemsList.value(selProblem):
- ProbResultsNames:不存在
- 概率结果类型:不存在
编辑 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
};
一个简单的测试证明 QMap
按预期工作:
QMap<int, QString> mm;
mm.insert(1, "Test1");
qDebug() << mm[1]; // "Test1"
mm.remove(1);
qDebug() << mm[1]; // "" (default constructed value)
mm.insert(1, "Test2");
qDebug() << mm[1]; // "Test2"
也就是说你的代码有问题。
这个说法本身就很可疑:
That last line appears to be doing nothing!
因为你继续说地图仍然包含 "old value"。但是你删除了那个键,所以如果 insert()
方法不起作用,你不应该得到旧值,而是一个默认的构造值。
这意味着问题很可能是 nProblem
与之前关联到映射中该键的值相同。地图有效,您的值可能有误。
找到问题了!我没有在 ProblemSets class.
的复制方法中声明两个变量
解决了简单地将它们添加到复制方法
MainWindow::ProblemSets::ProblemSets(const ProblemSets& other)
{
// copy
index = other.index;
usingBenchmark = other.usingBenchmark;
functionSelected = other.functionSelected;
info = other.info;
probVars_name = other.probVars_name;
probVars_type = other.probVars_type;
probVars_min = other.probVars_min;
probVars_max = other.probVars_max;
probVars_stpSize = other.probVars_stpSize;
probVars_stp = other.probVars_stp;
//here
probResultsNames = other.probResultsNames;
probResultsTypes = other.probResultsTypes;
//
varsNumber = other.varsNumber;
resNumber = other.resNumber;
}
我之前在使用 std::vector class 时遇到过这个问题,这就是为什么我怀疑可能是那个问题。感谢所有帮助过的人!
我有一个在任何地方都找不到的问题。我有一个 QMap 忽略 QMap.insert(Key, Value) 命令。这是代码:
//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 值 - 与已删除索引相同的值 -。我检查了调试,所有索引和变量都正确,但是当 .insert 命中时,没有任何反应。
最尴尬的是这段代码是 copy/paste 我用我正在使用的另一种方法制作的,它做类似的事情,只是改变变量名,但那个有效。
编辑 1: 这是 nProblem, 的内容selProb 和 problemsList.value(selProblem)
行前:
problemsList.insert(selProblem, nProblem);
selProb: 0
n问题:
- 概率结果名称:"NewRow0"
- 概率结果类型:"Real"
problemsList.value(selProblem):
- ProbResultsNames:不存在
- 概率结果类型:不存在
行后
problemsList.insert(selProblem, nProblem);
selProb: 0
n问题:
- 概率结果名称:"NewRow0"
- 概率结果类型:"Real"
problemsList.value(selProblem):
- ProbResultsNames:不存在
- 概率结果类型:不存在
编辑 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
};
一个简单的测试证明 QMap
按预期工作:
QMap<int, QString> mm;
mm.insert(1, "Test1");
qDebug() << mm[1]; // "Test1"
mm.remove(1);
qDebug() << mm[1]; // "" (default constructed value)
mm.insert(1, "Test2");
qDebug() << mm[1]; // "Test2"
也就是说你的代码有问题。
这个说法本身就很可疑:
That last line appears to be doing nothing!
因为你继续说地图仍然包含 "old value"。但是你删除了那个键,所以如果 insert()
方法不起作用,你不应该得到旧值,而是一个默认的构造值。
这意味着问题很可能是 nProblem
与之前关联到映射中该键的值相同。地图有效,您的值可能有误。
找到问题了!我没有在 ProblemSets class.
的复制方法中声明两个变量解决了简单地将它们添加到复制方法
MainWindow::ProblemSets::ProblemSets(const ProblemSets& other)
{
// copy
index = other.index;
usingBenchmark = other.usingBenchmark;
functionSelected = other.functionSelected;
info = other.info;
probVars_name = other.probVars_name;
probVars_type = other.probVars_type;
probVars_min = other.probVars_min;
probVars_max = other.probVars_max;
probVars_stpSize = other.probVars_stpSize;
probVars_stp = other.probVars_stp;
//here
probResultsNames = other.probResultsNames;
probResultsTypes = other.probResultsTypes;
//
varsNumber = other.varsNumber;
resNumber = other.resNumber;
}
我之前在使用 std::vector class 时遇到过这个问题,这就是为什么我怀疑可能是那个问题。感谢所有帮助过的人!