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」にマップされます)。

BGTEXpre-いいえ
BG TEXpremid-いいえ
BG TEXmid-background_texture
BGTEX投稿-background_texture
キー-background_texture、値-background_texture
BG TEX pre --background_texture
BG TEX premid-background_texture
BG TEXmid-level_height
BGTEXポスト-600
BG TEXpre-600
BGTEXプレミッド-600
BG TEXmid-level_width
BGTEXポスト-400

ご覧のとおり、値は「premid」と「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

キーの下に保存されている値の代わりに、マップ全体のデバッグ出力を毎回表示できますか?このような動作の理由の1つは、一部のスロットがテーブルモデルから発信された信号をリッスンし、マップを変更することである可能性があります。

于 2011-07-15T07:57:15.100 に答える