1 添加一个QPushButton,和QTextEdit。点击按钮选择文件并且将文件里的内容显示在QTextEdit上。
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath()); if(!fileName.isEmpty()) { QFile *file=new QFile; file->setFileName(fileName); bool ok=file->open(QIODevice::ReadOnly); if(ok) { QTextStream in(file); ui->textEdit->setText(in.readAll()); file->close(); delete file; } else { QMessageBox::information(this,"Error Message","Open File:"+file->errorString()); return; }
如何使用QTextEdit读取文件!,布布扣,bubuko.com
原文地址:http://blog.csdn.net/taoerit/article/details/38238839