您的位置:首页 > 编程语言 > Qt开发

qt读取中文txt文件

2014-06-12 20:57 573 查看
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]
记录一下

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: