【发布时间】:2017-04-21 22:32:17
【问题描述】:
void LoadWindow::on_Load_Open_Button_clicked()
{
QString load_filepath = ui->Load_FilePath_TextBox->text(); //Get text from textBox
load_filepath.replace("\\","/"); //Replaces all '\' with '/' for filepath reading
QFile loadFile(load_filepath);
if (loadFile.open(QIODevice::ReadOnly)) //Open the file in read-only mode and only reads if file was opened properly
{
QTextStream in(&loadFile);
qDebug() << "line in = " << in.readLine(); //ReadOut 1
QString x = in.readLine();
qDebug() << "x = " << x; //ReadOut 2
ExtractInfo(x);
}else{
//error
}
loadFile.close();
}
ReadOut 1 显示读入文件,但 ReadOut 2 正在打印“”,因此未分配 x。我尝试了许多变体,包括 .append() 和串联,但仍未分配。包括所有必需的库。
欢迎任何帮助,这可能是愚蠢的。在 Qt 中完成。
问候
【问题讨论】:
-
文件的第二行必须为空,因此
x在第二次调用readLine后包含一个空字符串。或者,第一行之后没有内容,所以x包含一个“空”QString。 -
那么如果我初始化 x 变量并使用连接或附加,它会解决这个问题吗?
-
您可以显示您打开的文件的示例,我运行了相同的代码并且没有任何问题。我使用 Linux 和 Qt 5.8
-
我已经做了几次测试,我观察到唯一发生的情况是你显示文件只有一行