1

I've a question that couldn't find anywhere. I have a QMap that's ignoring the QMap.insert(Key, Value) command. Here's the code:

    //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

}

That last line appears to be doing nothing! I've even tried to use

problemsList.remove(selProblem);
problemList.insert(selProblem, nProblem);

but got a similar result: the map not being inserted at the index selProblem. It got inserted, but with an outdated value - the same one of the deleted index -. I've checked on Debug and all the indexes and variables are correct, but when the .insert hits, nothing happens.

The most awkward thing is that this code is a copy/paste that I made from another method that I'm using that does similar thing, just changing the variable names, but that one works.

EDIT 1: This is the contents of nProblem, selProb and problemsList.value(selProblem)

Just before the Line:

problemsList.insert(selProblem, nProblem);

selProb: 0

nProblem:

  • ProbResultsNames: "NewRow0"
  • ProbResultsType: "Real"

problemsList.value(selProblem):

  • ProbResultsNames: non-existent
  • ProbResultsType: non-existent

After the line

problemsList.insert(selProblem, nProblem);

selProb: 0

nProblem:

  • ProbResultsNames: "NewRow0"
  • ProbResultsType: "Real"

problemsList.value(selProblem):

  • ProbResultsNames: non-existent
  • ProbResultsType: non-existent

EDIT 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

};
  • Have you tried to first remove any value that was here before, then do the insert, just to see the behaviour ? – Elcan Mar 27 '18 at 14:38
  • 1
    How do you verify that the value `nProblem` is not inserted? – vahancho Mar 27 '18 at 14:41
  • @FlorentUguet Yes! I used the _.remove(selProblem)_ before trying to insert; the value got deleted from the QMap like expected, but when I inserted the `nProblem` it entered the same value that there was before the deletion. @vahancho I used breakpoints and the debug to check the value of `nProblem, selProblem` and `problemsList`. I'll edit the post to add the values on debug. – Jorge Augusto Santos Mar 27 '18 at 14:46
  • 1
    As far as I understand, the `problemsList` is a `QMap`, right? So why the question title refers to a `QMap`? And can you post the whole `ProblemSets` class code? – p-a-o-l-o Mar 27 '18 at 14:56
  • @p-a-o-l-o sorry for the typo! You are absolutely correct. – Jorge Augusto Santos Mar 27 '18 at 15:02
  • Please provide a single-file, complete and compileable example that reproduces the issue. – Kuba hasn't forgotten Monica Mar 28 '18 at 00:42

2 Answers2

1

A simple test proves that QMap works as expected:

  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"

Which means that the problem lies in your code.

This statement itself is highly suspicious:

That last line appears to be doing nothing!

Because then you go on to say that the map still contains the "old value". But you removed that key, so if the insert() method didn't work, you shouldn't be getting the old value, but a default constructed value.

Which means that the problem is most likely that nProblem has the same value as the one that is previously associated to that key in the map. The map works, you values are likely wrong.

dtech
  • 47,916
  • 17
  • 112
  • 190
1

Found the issue! I didn't have both the variables declared on the copy method of the ProblemSets class.

Solved simply adding them to the copy method

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;
}

I had this issue before with the std::vector class, and that's why I suspected that could be that. Thanks to everyone that helped!