qt读取中文txt文件

QAbstractItemModel *Widget::modelFromFile(const QString& fileName)
{
    QFile file(fileName);
//    qDebug()<<"aa";
//    qDebug()<<file.open(QFile::ReadOnly);
    if (!file.open(QFile::ReadOnly))
        return new QStringListModel(completer);
//    qDebug()<<"aa";
 
 
#ifndef QT_NO_CURSOR
    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
#endif
    QStringList words;
 
 
    QTextStream stream( &file );
    QString  line;
//     qDebug()<<"aa";
//    qDebug()<<!stream.atEnd();
    while ( !stream.atEnd() )
    {
//            qDebug()<<!stream.atEnd();
        line = stream.readLine(); // 不包括“\n”的一行文本
//            qDebug()<<line;
        words.append(line.trimmed());
    }
 
 
    file.close();
//    qDebug()<<words.size();
//    }
        qDebug()<<words.at(0)<<words.at(1)<<words.at(2)<<words.at(3)<<words.at(4)<<words.at(5);
 
 
 
 
#ifndef QT_NO_CURSOR
    QApplication::restoreOverrideCursor();
#endif
    return new QStringListModel(words, completer);
}
//! [2]


记录一下
 

你可能感兴趣的:(qt读取中文txt文件)