如何在 QT 中使用 QFile?

how can work with QFile in QT?

我从 QFile 中给出一个对象 class ... 但是在调试时我发现这没有价值...... 我该怎么办???

mainscrin.cpp

MainScrin::MainScrin(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainScrin){
ui->setupUi(this);

QMainWindow::statusBar()->showMessage("Welcome...",10000);
//ui->tableWidget->setStyle(Macintosh);
double lab22 = ui->tableWidget->StyledPanel;
ui->label_22->setNum(lab22);


QTableWidgetItem *item_1=new QTableWidgetItem;

QString Filename1("bin/bin_1.txt");
QStringList str_item1 = StRead(Filename1);

QString Filename2("bin/bin_2.txt");
QStringList str_item2 = StRead(Filename2);

QString Filename3("bin/bin_3.txt");
QStringList str_item3 = StRead(Filename3);

QString Filename4("bin/bin_4.txt");
QStringList str_item4 = StRead(Filename4);

QString Filename5("bin/bin_5.txt");
QStringList str_item5 = StRead(Filename5);


for(int i=0;i<str_item1.size();i++)
{
    item_1->setText(str_item1[i]);
    ui->tableWidget->setItem(i+1,1,item_1);
}

for(int i=0;i<str_item2.size();i++)
{
    item_1->setText(str_item1[i]);
    ui->tableWidget->setItem(i+1,2,item_1);
}

for(int i=0;i<str_item3.size();i++)
{
    item_1->setText(str_item1[i]);
    ui->tableWidget->setItem(i+1,3,item_1);
}

for(int i=0;i<str_item4.size();i++)
{
    item_1->setText(str_item1[i]);
    ui->tableWidget->setItem(i+1,4,item_1);
}

for(int i=0;i<str_item5.size();i++)
{
    item_1->setText(str_item1[i]);
    ui->tableWidget->setItem(i+1,5,item_1);
}
}

mainscrin.h

private:
    Ui::MainScrin *ui;
    QList<QString> StRead(QString Filename)
{
    QFile m_File(Filename);

    if(!m_File.open(QFile::ReadWrite | QFile::Text ))
    {
        QMessageBox message_file_Read;
        message_file_Read.warning(0,"Open Error"
               ,"coude not to open file for Reading");
        QList<QString> Erorr;
        return Erorr;
    }

    QList<QString> strings;

    QTextStream in(&m_File);
       while (!in.atEnd())
       {
          QString line = in.readLine();
          strings.push_back(line);
       }

    m_File.close();
    return strings;
}
};

我想,您的工作目录没有文件夹 'bin'。您可以使用 applicationFilePath 将相对路径转换为绝对路径。

QString QCoreApplication::applicationFilePath()

另外,请看一下 QFile 用法的简单示例 (QFile):

QFile file("/proc/modules");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    return;

QTextStream in(&file);
QString line = in.readLine();
while (!line.isNull()) {
    process_line(line);
    line = in.readLine();
}

检查 IDE 中的工作目录。在大多数情况下,工作目录与您启动可执行文件的位置不同。