2011-07-15 9 views
0

"table-> setItem(index、0、widg);"望ましくない副作用を有するステートメント。Qt QMapの値を変更するテーブルの設定を変更する

私は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++; 
    } 
} 

恩赦デバッグテキストのすべてを、それは、私は、問題がある場所を正確に絞り込むことができた方法です。あなたが見ることができるように「事前半ば」の間の値の変化と「ミッド」デバッグ文は、

 
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 

:ここでは機能の実行時の出力の一部(「background_textureは」当初の「ハズレ」にマッピング)ですつまり、 "table-> setItem(index、0、widg);"というステートメントを実行すると、最新のi.value()が何であれ、 "background_texture"キーの値を変更します。

なぜですか?

編集:フルデバッグ情報:

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

を生成しますが、全体マップのデバッグ出力するたびに代わりのキーの下に格納された値を表示することができますか? このような振る舞いの1つの理由は、一部のスロットがテーブルモデルによって生成された信号をリッスンし、マップを変更することが原因である可能性があります。

+0

あなたは正しいですが、私はupdatePlist()関数の先頭に "noEmit"ブール値を設定し、問題を解決するために他のスロットの先頭にtrueに設定されていないことを確認しなければなりません。 – numegil

関連する問題