在构造函数中定义 一个QTextEdit对象:
m_pText = new QTextEdit(this);
实现函数如下:
QString fileName = QFileDialog::getOpenFileName(this,tr("Open a File"),".",tr("Text File(*.txt)));
if(fileName.length() == 0)
QMessageBox::information(this,tr("Text Files"),tr("You have not open any file"));
else
{
QDir *pDir = new QDir(".");
QString fileDir = pDir->filePath(fileName);
QFile file(fileName);
if (!file.open(QIODevice::ReadWrite))
return;
QTextStream out(&file);
while(!file.atEnd())
{
m_pText->setText(out.readAll());
}
}