从 Qt 字符串打开文件

发布于 2024-09-06 22:20:58 字数 282 浏览 2 评论 0原文

我正在制作一个 Qt 应用程序,并且有一个用于打开文件的按钮,该文件连接到自定义插槽。这是到目前为止的槽代码:

void MainWindow::file_dialog() {
    const QFileDialog *fd;
    const QString filename = fd->getOpenFileName();
}

我怎样才能将文件名转换为 const char *,打开文件,读取它并将文本存储在 QString 中,然后关闭文件。我正在使用Qt4。

I am making a Qt application and I have a button to open a file, which is connected to a custom slot. This is the slot code so far:

void MainWindow::file_dialog() {
    const QFileDialog *fd;
    const QString filename = fd->getOpenFileName();
}

How could I have it then convert the file name to a const char *, open the file, read it and store the text in a QString, and then close the file. I am using Qt4.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感受沵的脚步 2024-09-13 22:20:58

要读取文件的内容,您可以执行此操作

QString filename = QFileDialog::getOpenFileName();

QFile file(filename);
 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
     return;

QString content = file.readAll();

file.close();

To read the contents of a file, you can do this:

QString filename = QFileDialog::getOpenFileName();

QFile file(filename);
 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
     return;

QString content = file.readAll();

file.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文