Qt迭代器无法访问正确的对象 [英] Qt iterator not accessing the correct object

查看:276
本文介绍了Qt迭代器无法访问正确的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

问题

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

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?

代码

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

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

这是exec函数,如果anyValueChanged是true:

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

这是以前调用的名为bindToQuery的函数:

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


推荐答案

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;

这篇关于Qt迭代器无法访问正确的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆