【问题标题】:Qt QMap<int, MyClass> ignores insert commandQt QMap<int, MyClass> 忽略插入命令
【发布时间】: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: 这是 nProblemselProbproblemsList.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, selProblemproblemsList 的值。我将编辑帖子以添加调试值。
  • 据我了解,problemsListQMap&lt;int, ProblemSets&gt;,对吧?那么为什么问题标题指的是QMap&lt;int, QString&gt;?你能发布整个ProblemSets 课程代码吗?
  • @p-a-o-l-o 抱歉打错了!你是绝对正确的。

标签: qt insert qmap


【解决方案1】:

一个简单的测试证明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"

这意味着问题出在你的代码上。

这句话本身就很可疑:

最后一行似乎什么也没做!

因为你接着说地图仍然包含“旧值”。但是你删除了那个键,所以如果 insert() 方法不起作用,你不应该得到旧值,而是一个默认构造值。

这意味着问题很可能是nProblem 的值与之前与映射中该键关联的值相同。该地图有效,您的值可能是错误的。

【讨论】:

    【解决方案2】:

    找到问题了!我没有在 ProblemSets 类的复制方法上声明这两个变量。

    只需将它们添加到复制方法即可解决

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 2011-06-10
      • 2023-03-13
      相关资源
      最近更新 更多