I have a file with 3000 strings (1 string-few words). I need to read the strings into a QList. How can I do that? I have tried the following:

1.txt
string
string2

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

有帮助吗?

解决方案

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;
}

Building (something similar)

g++ -fPIC -I/usr/include/qt -I/usr/include/qt/QtCore -lQt5Core main.cpp

Output

("string", "string2")
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top