Qt迭代器无法访问正确的对象

时间:2013-06-14 10:18:28

标签: qt iterator dereference

我有一个Hash映射,其值是名为ModelBinding的类的列表(qlist)。这个类有三个属性,其中一个是我改变的:值。由于我不理解的原因,我的代码是复制对象并修改该副本,相反,我希望它修改该实例而不复制它。

问题

在我看来,我正在取消引用该对象并进行更改。但是,显然,更改副本而不是实例。这是为什么?我该如何更改实例呢?

代码

这是修改(或应该)实例值的函数:

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

这是exec函数,如果anyValueChanged为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);
}

这是名为bindToQuery的函数,之前调用过:

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

从调试消息中,值显然永远不会改变:

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=?" 

1 个答案:

答案 0 :(得分:1)

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

在编辑对象之前,您正在制作对象的副本。使用:

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