【问题标题】:I would like to open and read a text file at program launch, using Qt我想使用 Qt 在程序启动时打开并读取文本文件
【发布时间】:2013-10-16 07:22:31
【问题描述】:

我想在程序启动时使用 Qt 打开一个文本文件。我希望文本出现在名为 textEdit 的文本字段中。

这是一个简单的记事本程序,我正在将它改成一个我想做其他特殊事情的应用程序。

如何在程序启动时在我的 textEdit 小部件中输入文本文件,例如“text.txt”?所有的文本文件。

用 C++ 编写。

谢谢。

【问题讨论】:

  • 阅读 Qt 初学者指南。据我记得有一个 textEditor 示例。一些指针:QFileQTextDocument::setPlaintext()
  • 你指的初学者指南是什么?
  • this example 怎么样。

标签: c++ c qt user-interface io


【解决方案1】:
#include <QFile>
#include <QTextStream>

QString fileName = "myFile.txt";
File* myFile = new QFile(fileName);
if (myFile->open(QIODevice::ReadOnly | QIODevice::Text)
{
    QTextStream *myFileStream = new QTextStream(myFile);
    while ( !(myFileStream->atEnd()) )
    {
        QString line = myFileStream->readLine();
        textEdit->append(line);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多