У меня проблема с "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 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
Как видите, значение изменяется между отладочными операторами «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"))