gpt4 book ai didi

c++ - 如何使用文本文件中的内容填充 Qt ListView?

转载 作者:太空狗 更新时间:2023-10-29 20:39:15 26 4
gpt4 key购买 nike

我有一个包含很多词汇的文本文件,每个词汇用一个新行分隔。

如何使用文本文件中的词汇填充 Qt ListView?

最佳答案

QStringListModel *model;

// Create model
model = new QStringListModel(this);

QStringList stringList;

// open the file
QFile textFile("/<FullPath>/<fileName>");
if(!textFile.open(QIODevice::ReadOnly)) {
QMessageBox::information(0,"Error",textFile.errorString());
}

// teststream to read from file
QTextStream textStream(&textFile);
while (true)
{
QString line = textStream.readLine();
if (line.isNull())
break;
else
stringList.append(line); // populate the stringlist
}

// Populate the model
model->setStringList(stringList);

// Glue model and view together
ui->listView->setModel(model);

// if you want to add additional feature to listview.
ui->listView->
setEditTriggers(QAbstractItemView::AnyKeyPressed |
QAbstractItemView::DoubleClicked);

关于c++ - 如何使用文本文件中的内容填充 Qt ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28408542/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com