因此,我试图通过执行以下操作将QFile转换为QString:
void MainWindow::openTemplateFile(QString location)
{
if (location.isEmpty())
return;
else
{
QString variable;
templateFile.setFileName(location);
if (!templateFile.open(QFile::ReadOnly | QFile::Text))
{
QMessageBox::information(this, "Unable to open template",
templateFile.errorString());
return;
}
else // file opened and ready to read from
{
QTextStream in(&templateFile);
QString fileText = in.readAll();
qDebug() << templateFile.size() << in.readAll();
}
}
}但是,在调试控制台中,我得到了以下结果:
48 ""templateFile确实存在,并且是MainWindow类的一部分。这也是简化的代码-在实际程序中,我从文件中读取字符,它正确工作。位置字符串是QFileDialog::getOpenFileName函数的结果,我用该函数打开了一个txt文件。
发布于 2017-11-07 01:12:30
你给readAll()打了两次电话。第二次,流定位在文件末尾,因此readAll()没有什么可读取的,并返回一个空字符串。而是在调试输出中打印fileText。
https://stackoverflow.com/questions/47148123
复制相似问题