Qt-QMap<;int,MyClass>;忽略insert命令

Qt-QMap<;int,MyClass>;忽略insert命令,qt,insert,qmap,Qt,Insert,Qmap,我有一个哪儿都找不到的问题。我有一个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 val

我有一个哪儿都找不到的问题。我有一个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

}
但是得到了一个类似的结果:地图没有被插入索引。它被插入了,但是有一个过时的值——与已删除索引中的值相同。我已经检查了Debug,所有的索引和变量都是正确的,但是当.insert命中时,什么也没有发生

最令人尴尬的是,这段代码是我从另一个方法复制/粘贴的,我正在使用的另一个方法做类似的事情,只是更改变量名,但这个方法有效

编辑1:这是nProblemselprobleproblemsList.value(selproble)

就在排队前:

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

};
类问题集
{
公众:
问题集();
虚拟问题集();
问题集(常量问题集和其他);
问题集和运算符=(常量问题集和其他);
//为了避免污染工作岗位,我把接球手和接球手藏起来
私人:
整数指数;
使用基准的bool;
选择QString函数;
QString信息;
QMap probVars_名称、probVars_类型、ProbResultsName、ProbResultsType;
QMap probVars_min,probVars_max;
QMap probVars_stp size,probVars_stp;
int varsNumber;//保存已创建的变量数,仅用于显示目的
int resNumber;//保存已创建的结果数量,仅用于显示目的
};

一个简单的测试证明
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"
QMap-mm;
插入(1,“测试1”);

qDebug()找到了问题!我没有在ProblemSets类的copy方法上声明这两个变量

解决了简单地将它们添加到复制方法中的问题

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类中遇到过这个问题,这就是为什么我怀疑可能是这样。感谢所有帮助过我的人

您是否尝试过先删除在此之前存在的任何值,然后进行插入,只是为了查看其行为?如何验证值
nProblem
未插入?@FlorentUguet Yes!在尝试插入之前,我使用了.remove(selProblem);该值像预期的那样从QMap中删除,但当我插入
nProblem
时,它输入的值与删除之前的值相同@vahancho I使用断点和调试来检查
nProblem、selProblem
problemsList
的值。我将编辑帖子以添加调试时的值。据我所知,
problemsList
是一个
QMap
,对吗?那么,为什么问题标题指的是
QMap
?你能发布整个
问题集
类代码吗?@p-a-o-l-o对不起,输入错误!你完全正确。
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;
}