Search code examples
qtinsertqmap

Qt QMap<int, MyClass> ignores insert command


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

};

Solution

  • 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!