You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发宝可梦类型项目:从TXT指定行生成QStringList用于ListView展示

Hey there! Let's work through this Pokémon type data issue you're facing with Qt. I’ve dealt with similar file I/O and UI binding tasks before, so here’s a straightforward approach that avoids hardcoding and properly parses your TXT structure.

First, let’s recap your file format to make sure we’re aligned: each "group" of 3 lines represents one type’s data:

  1. Line 1: The type selected in the combo box
  2. Line 2: Types this one beats (comma-separated)
  3. Line 3: Types this one is weak to (comma-separated)

Step 1: Read the Entire TXT File into Memory

Instead of targeting specific line numbers (which breaks easily if your file ever changes), we’ll read all valid lines first. This handles accidental empty lines and keeps our parsing logic flexible.

#include <QFile>
#include <QTextStream>
#include <QStringList>
#include <QMessageBox>

// Assuming this is in your widget/class method
QFile typeFile("pokemon_types.txt");
if (!typeFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
    QMessageBox::warning(this, "Error", "Failed to open type data file:\n" + typeFile.errorString());
    return;
}

QTextStream inputStream(&typeFile);
QStringList allValidLines;
while (!inputStream.atEnd()) {
    QString line = inputStream.readLine().trimmed();
    // Skip empty lines to avoid parsing issues
    if (!line.isEmpty()) {
        allValidLines.append(line);
    }
}
typeFile.close();

Step 2: Parse Lines into Structured Data

Let’s create a simple struct to store each type’s info, then parse the lines in groups of 3:

// Define this struct either in your class header or above the parsing code
struct PokemonTypeInfo {
    QString selectedType;
    QStringList strengths;
    QStringList weaknesses;
};

QList<PokemonTypeInfo> allTypeData;

// Parse in groups of 3 lines
for (int i = 0; i < allValidLines.size(); i += 3) {
    // Check if we have a complete group (avoid out-of-bounds errors)
    if (i + 2 >= allValidLines.size()) {
        QMessageBox::warning(this, "Warning", "Incomplete data group found in the TXT file");
        break;
    }

    PokemonTypeInfo currentType;
    currentType.selectedType = allValidLines[i];
    // Split comma-separated strings into QStringLists
    currentType.strengths = allValidLines[i+1].split(", ", Qt::SkipEmptyParts);
    currentType.weaknesses = allValidLines[i+2].split(", ", Qt::SkipEmptyParts);

    allTypeData.append(currentType);
}

Step 3: Populate Your Combo Box

Now we’ll fill the combo box with all the selected types from our parsed data:

// Assuming your combo box is named `typeComboBox`
typeComboBox->clear();
for (const auto& typeInfo : allTypeData) {
    typeComboBox->addItem(typeInfo.selectedType);
}

Finally, we’ll connect the combo box’s selection change to update your list views with the corresponding strengths/weaknesses. We’ll use QStringListModel since it’s the easiest way to bind string lists to QListView:

#include <QStringListModel>
#include <QComboBox>
#include <QListView>

// Connect the combo box's selection signal
connect(typeComboBox, &QComboBox::currentIndexChanged, this, [this](int selectedIndex) {
    if (selectedIndex < 0 || selectedIndex >= allTypeData.size()) {
        return;
    }

    const PokemonTypeInfo& selectedInfo = allTypeData[selectedIndex];

    // Update strengths list view (assuming it's named `strengthsListView`)
    QStringListModel* strengthModel = new QStringListModel(selectedInfo.strengths, this);
    strengthsListView->setModel(strengthModel);

    // Update weaknesses list view (assuming it's named `weaknessesListView`)
    QStringListModel* weaknessModel = new QStringListModel(selectedInfo.weaknesses, this);
    weaknessesListView->setModel(weaknessModel);
});

// Trigger the first selection manually if needed
if (!allTypeData.isEmpty()) {
    typeComboBox->setCurrentIndex(0);
}

Quick Optimization Tip

If you want to avoid creating a new model every time the selection changes (more efficient for larger datasets), initialize a single model for each list view upfront, then just update its string list:

// Initialize these models once (e.g., in your widget's constructor)
QStringListModel* strengthModel = new QStringListModel(this);
QStringListModel* weaknessModel = new QStringListModel(this);
strengthsListView->setModel(strengthModel);
weaknessesListView->setModel(weaknessModel);

// Then in the selection handler:
strengthModel->setStringList(selectedInfo.strengths);
weaknessModel->setStringList(selectedInfo.weaknesses);

内容的提问来源于stack exchange,提问作者Max

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 11:44:07