2

I'm trying to read a text file and display the contents in a QPlainTextEdit. Please can you point out what I'm doing wrong:

QFile jsonFile("data.json");  

if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
    qDebug() << "Failed to open file";
    qDebug() << jsonFile.errorString();
    return;
}
else
{
    qDebug() << "File opened";
} //It returns that the file opened successfully

qDebug() << "File Exists?: " << jsonFile.exists(); //Yep, it exists.

QTextStream outStream(&jsonFile);
QString textString = outStream.readAll();
qDebug() << "Text string: " << textString; //textString is empty! ""
ui->fileToPost->setPlainText(textString); //fileToPost is the QPlainTextEdit
jsonFile.close();

If I do something like

QString textString = "The cat sat on the mat";

it displays fine. The problem is that nothing is being read from the stream (or maybe the file).

4

1 回答 1

1

尝试检查文件的绝对路径,可能不是您期望的位置:qDebug()<<QFileInfo("data.json").absoluteFilePath();

于 2013-07-16T13:46:06.563 回答