2011-07-15 60 views
0

我有一个问题,“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")) 
+0

有趣。迭代之前和之后的qDebug()<< levelPList是什么意思? –

回答

0

你能每次都显示整个地图的调试输出,而不是项下存储的值吗? 这种行为的一个原因可能是某些插槽监听由表模型发出的信号并修改地图。

+0

你是对的,我必须在updatePlist()函数的开头设置一个“noEmit”布尔值,并确保它在我的其他插槽的开头没有设置为true来解决问题。 – numegil