qt打开和编辑文件

本文档介绍了如何使用Qt的QFileDialog打开文件,并通过QFile和QTextStream读取文件内容显示在文本编辑器中。同时,展示了如何实现保存文件功能,利用QFileDialog让用户指定保存位置,并使用QTextStream将文本编辑器的内容写入文件。
摘要由CSDN通过智能技术生成

Opening Files

In this section, we implement the functionality of the on_actionOpen_triggered() slot. The first step is asking the user for the name of the file to open. Qt comes with QFileDialog, which is a dialog from which the user can select a file. The appearance of the dialog depends on the desktop platform that you run the application on. The following image shows the dialog on macOS:

We complement the code generated by Qt Designer in notepad.cpp, as follows:

void Notepad::on_actionOpen_triggered()
{
    
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(),
            tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));

    if (!fileName.isEmpty()) {
    
        QFile file(fileName);
        if
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值