简体   繁体   English

从QTextStream读取Qt

[英]Qt reading from QTextStream

I'm trying to read a text file and display the contents in a QPlainTextEdit. 我正在尝试读取文本文件并在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). 问题是没有从流(或文件)中读取任何内容。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM