Search code examples
qtiteratordereference

Qt iterator not accessing the correct object


I have a Hash map whose values are list (qlist) of a class called ModelBinding. This class has three attributes, one of which I change: value. Fro a reason which I don't understand, my code is copying the object and modifying that copy, when instead, I would like it to modify that instance without copying it.

Question

It seems to me that I am dereferencing the object and changing it. However, clearly, a copy is changed instead of the instance. Why is that? How can I change the instance instead?

Code

Here's the function which modifies (or should) the value of the instance:

void SqlQueryModel::updateBindings(QString modelName, QString col, QVariant val)
{
    qDebug() << "Got signal from model" << modelName << "col"<<col<<"changed to"<< val;

    /** Now, let's go through all the models associated to this instance.
     * We're going to see if the new signal we got is used for this model (for model name and column name).
     * If so, we'll assigned it (cf. annotation A1). Then, we'll execute this query by calling exec().
    **/

    bool anyValueChanged = false;

    QHash<QString, QList<ModelBinding> >::iterator bindingsIt;
    for (bindingsIt = bindings.begin(); bindingsIt != bindings.end(); bindingsIt++){

        QList<ModelBinding>::iterator eachBindingIt;
        QList<ModelBinding> curBinding = bindingsIt.value();

        for(eachBindingIt = curBinding.begin(); eachBindingIt != curBinding.end(); eachBindingIt++){
            ModelBinding binding = *eachBindingIt;
            if(bindingsIt.key() == modelName && binding.column == col){
                binding.value = val;
                binding.hasBeenChanged = true;
                anyValueChanged = true;
            }
        }
    }
    if (anyValueChanged){
        this->exec();
    }
}

Here's the exec function, which is called if anyValueChanged is true:

void SqlQueryModel::exec()
{
    /* Let's create a QSqlQuery. It will store the query and we'll bind values to it.*/
    QSqlQuery sQuery;
    /* If we initialize the query with the string, then we CANNOT use bind (won't work and won't show any error).*/
    sQuery.prepare(this->query);

    /** Now, let's go through all the models associated to this instance.
     * For each of them, we'll bind its value.
    **/

    QHash<QString, QList<ModelBinding> >::iterator bindingsIt;
    for (bindingsIt = bindings.begin(); bindingsIt != bindings.end(); bindingsIt++){
        QList<ModelBinding>::iterator eachBindingIt;
        QList<ModelBinding> curBinding = bindingsIt.value();

        for(eachBindingIt = curBinding.begin(); eachBindingIt != curBinding.end(); eachBindingIt++){
            ModelBinding binding = *eachBindingIt;
            binding.bindToQuery(&sQuery);
        }
    }

    /* Let's not forget to execute this query, or nothing will be displayed in the QML. */
    sQuery.exec();
    qDebug() << sQuery.lastQuery();
    QMapIterator<QString, QVariant> i(sQuery.boundValues());
    while (i.hasNext()) {
        i.next();
        qDebug() << i.key().toAscii().data() << "="
                 << i.value().toString().toAscii().data();
    }
    this->setQuery(sQuery);
}

Here's the function called bindToQuery which is called previously:

void ModelBinding::bindToQuery(QSqlQuery *sQuery)
{
    sQuery->bindValue(placeholder, value);
    qDebug() << "changed?" << hasBeenChanged;
    if(sQuery->boundValue(placeholder) != value){
        qDebug() << "Binding error: " << sQuery->boundValue(placeholder) << "!="
                 << value << "for" << placeholder;
    }else{
        qDebug() << placeholder << "binding successful with value"<<value;
    }
}

From the debug messages the value is clearly never changed:

Got signal from model "tcModel" col "TLM_NO" changed to QVariant(QString, "AC00100") 
changed? true 
":tm" binding successful with value QVariant(QString, "AC01040") 
"SELECT * from tl04 WHERE TLM_NO=:tm" 
:tm = AC01040 
Generating role names. 
No error with query:
 "SELECT * from tl04 WHERE TLM_NO=?" 

Solution

  • QList<ModelBinding> curBinding = bindingsIt.value();
    ...
    ModelBinding binding = *eachBindingIt;
    

    You're making a copy of the objects before editing them. Use:

    QList<ModelBinding>& curBinding = *bindingsIt;
    ...
    ModelBinding& binding = *eachBindingIt;