我有一个问题,“table-> setItem(index,0,widg);”声明有不需要的副作用。Qt设置表项更改QMap的值
我有地图将QString一来将QString:
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”最初映射到“不”):
BG TEX pre - nope BG TEX pre mid - nope BG TEX mid - background_texture BG TEX post - background_texture Key - background_texture, Value - background_texture BG TEX pre - background_texture BG TEX pre mid - background_texture BG TEX mid - level_height BG TEX post - 600 BG TEX pre - 600 BG TEX pre mid - 600 BG TEX mid - level_width BG TEX post - 400
正如你所看到的,“前中期”和“中”调试语句之间的价值变化,这意味着执行“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"))
有趣。迭代之前和之后的qDebug()<< levelPList是什么意思? –