将txt文件读入QStringList

pun*_*sta 3 c++ qt qfile qtcore

我有一个3000字符串的文件(1个字符串 - 几个字).我需要把字符串读成一个QList.我怎样才能做到这一点?我尝试过以下方法:

1.txt
string
string2
Run Code Online (Sandbox Code Playgroud)

function()<=> MyList<<"string"<<"string2";

lpa*_*app 6

main.cpp中

#include <QStringList>
#include <QFile>
#include <QTextStream>
#include <QDebug>

int main(int argc, char **argv)
{
    QString fileName = "foo.txt"; // or "/absolute/path/to/your/file"
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return 1;

    QStringList stringList;
    QTextStream textStream(&file);

    while (!textStream.atEnd())
        stringList << textStream.readLine();

    file.close();

    qDebug() << stringList;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

建筑(类似的东西)

g++ -fPIC -I/usr/include/qt -I/usr/include/qt/QtCore -lQt5Core main.cpp
Run Code Online (Sandbox Code Playgroud)

产量

("string", "string2")
Run Code Online (Sandbox Code Playgroud)