0

我对“table->setItem(index, 0, widg);”有疑问 声明有不想要的副作用。

我有 QStrings 到 QStrings 的映射:

QMap<QString, QString> levelPlist;

稍后,我有一个函数可以用此映射中包含的键和值填充 QTableWidget。所以,我有以下功能:

void MainWindow::updateLevelPlistTable()
{
    QTableWidget *table = ui->levelPlistTableWidget;

    int count = levelPlist.count();
    table->setRowCount(count);
    table->setColumnCount(2);


    QMap<QString, QString>::const_iterator i;
    int index = 0;

    for (i = levelPlist.constBegin(); i != levelPlist.constEnd(); ++i)
    {
        qDebug(QString("BG TEX pre  - " + levelPlist.value("background_texture")).toAscii());
        QTableWidgetItem *widg = new QTableWidgetItem(i.key());
        qDebug(QString("BG TEX pre mid  - " + levelPlist.value("background_texture")).toAscii());
        table->setItem(index, 0, widg);
        qDebug(QString("BG TEX mid  - " + levelPlist.value("background_texture")).toAscii());
        table->setItem(index, 1, new QTableWidgetItem(i.value()));

        qDebug(QString("BG TEX post - " + levelPlist.value("background_texture")).toAscii());
        if(index == 0)
        {
            qDebug(QString("Key - " + i.key() + ", Value - " + i.value()).toAscii());
        }
        index++;
    }
}

请原谅所有调试文本,但这就是我能够准确缩小问题所在的方式。这是函数运行时的一些输出(“background_texture”最初映射到“nope”):

BG TEX pre - nope
BG TEX pre mid - nope
BG TEX 中 - background_texture
BG TEX 帖子 - background_texture
键 - 背景纹理,值 - 背景纹理
BG TEX pre - background_texture
BG TEX pre mid - background_texture
BG TEX 中级高度
BG TEX 后 - 600
BG TEX pre-600
BG TEX 前中期 - 600
BG TEX 中级宽度
BG TEX 后 - 400

可以看到,“pre mid”和“mid”调试语句之间的值是变化的,也就是说执行“table->setItem(index, 0, widg);”的语句 将“background_texture”键的值更改为最新的 i.value()。

为什么?

编辑:完整的调试信息:

void MainWindow::updateLevelPlistTable()
{
    QTableWidget *table = ui->levelPlistTableWidget;

    int count = levelPlist.count();
    table->setRowCount(count);
    table->setColumnCount(2);


    QMap<QString, QString>::const_iterator i;
    int index = 0;

    for (i = levelPlist.constBegin(); i != levelPlist.constEnd(); ++i)
    {
        QTableWidgetItem *widg = new QTableWidgetItem(i.key());
        table->setItem(index, 0, widg);
        qDebug() << "before - " << levelPlist;
        table->setItem(index, 1, new QTableWidgetItem(i.value()));
        qDebug() << "after - " << levelPlist << "\n";
        index++;
    }
}

生产

before -  QMap(("background_texture", "background_texture")("level_height", "600")("level_width", "400")("start_x", "250")("start_y", "50")("world", "null")) 
after -  QMap(("background_texture", "background_texture")("level_height", "600")("level_width", "400")("start_x", "250")("start_y", "50")("world", "null")) 

before -  QMap(("background_texture", "level_height")("level_height", "600")("level_width", "400")("start_x", "250")("start_y", "50")("world", "null")) 
after -  QMap(("background_texture", "600")("level_height", "600")("level_width", "400")("start_x", "250")("start_y", "50")("world", "null")) 

before -  QMap(("background_texture", "level_width")("level_height", "600")("level_width", "400")("start_x", "250")("start_y", "50")("world", "null")) 
after -  QMap(("background_texture", "400")("level_height", "600")("level_width", "400")("start_x", "250")("start_y", "50")("world", "null")) 

before -  QMap(("background_texture", "start_x")("level_height", "600")("level_width", "400")("start_x", "250")("start_y", "50")("world", "null")) 
after -  QMap(("background_texture", "250")("level_height", "600")("level_width", "400")("start_x", "250")("start_y", "50")("world", "null")) 

before -  QMap(("background_texture", "start_y")("level_height", "600")("level_width", "400")("start_x", "250")("start_y", "50")("world", "null")) 
after -  QMap(("background_texture", "50")("level_height", "600")("level_width", "400")("start_x", "250")("start_y", "50")("world", "null")) 

before -  QMap(("background_texture", "world")("level_height", "600")("level_width", "400")("start_x", "250")("start_y", "50")("world", "null")) 
after -  QMap(("background_texture", "null")("level_height", "600")("level_width", "400")("start_x", "250")("start_y", "50")("world", "null")) 
4

1 回答 1

0

您可以每次显示整个地图的调试输出而不是存储在键下的值吗?这种行为的一个原因可能是某些插槽监听了表模型发出的信号并修改了映射。

于 2011-07-15T07:57:15.100 回答