gpt4 book ai didi

c++ - Qt 从 QTextStream 读取

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:27:04 24 4
gpt4 key购买 nike

我正在尝试读取文本文件并在 QPlainTextEdit 中显示内容。请你指出我做错了什么:

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();

如果我做类似的事情

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

它显示正常。问题是没有从流(或文件)中读取任何内容。

最佳答案

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

关于c++ - Qt 从 QTextStream 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17676862/

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