if(ui->comboBox->currentIndex()==0) QFile file("1.txt");
else if (ui->comboBox->currentIndex() == 1) QFile file("2.txt");
else if (ui->comboBox->currentIndex() == 2) QFile file("3.txt");
else if (ui->comboBox->currentIndex() == 3) QFile file("3.txt");
大佬们后面该怎么样将读取的文件显示在listview这个控件上ehttps://img-ask.csdn.net/upload/201907/13/1563021577_779357.png
就是在空白处显示文本文件的内容
你问的问题描述不够清楚,你的文件是怎么样的没描述,是否是读一行将文字(还是数字)显示listview的一个item?
在.h;
1.添加
#include <QStandardItemModel>
2.在类内添加
QStandardItemModel *model;
.cpp
1.构造函数添加
model = new QStandardItemModel(this);
ui->listView->setModel(model);
2.槽函数添加
void Widget::on_pushButton_clicked()
{
QString filePath = "";
if(ui->comboBox->currentIndex()==0)
{
filePath = "1.txt";
}
else if (ui->comboBox->currentIndex() == 1)
{
filePath = "2.txt";
}
else if (ui->comboBox->currentIndex() == 2)
{
filePath = "3.txt";
}
else if (ui->comboBox->currentIndex() == 3)
{
filePath = "4.txt";
}
if(filePath != "")
{
QFile file(filePath);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "open file failed!!!";
return;
}
while (!file.atEnd()) {
QByteArray arr = file.readLine();
QStandardItem *item = new QStandardItem(QString(arr));
model->appendRow(item);
}
file.close();
}
}