Qt(八)文本文件(打开、修改与保存)

这篇具有很好参考价值的文章主要介绍了Qt(八)文本文件(打开、修改与保存)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


演示文本文件的打开,修改与保存。主要是熟悉相应的类代码使用。

一、UI布局

qt 保存文本文件,Qt/C++,qt,ui,开发语言

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>763</width>
    <height>541</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout_2">
    <item row="0" column="0">
     <widget class="QTabWidget" name="tabWidget">
      <property name="palette">
       <palette>
        <active>
         <colorrole role="Text">
          <brush brushstyle="SolidPattern">
           <color alpha="255">
            <red>170</red>
            <green>0</green>
            <blue>255</blue>
           </color>
          </brush>
         </colorrole>
         <colorrole role="PlaceholderText">
          <brush brushstyle="SolidPattern">
           <color alpha="128">
            <red>170</red>
            <green>0</green>
            <blue>255</blue>
           </color>
          </brush>
         </colorrole>
        </active>
        <inactive>
         <colorrole role="Text">
          <brush brushstyle="SolidPattern">
           <color alpha="255">
            <red>170</red>
            <green>0</green>
            <blue>255</blue>
           </color>
          </brush>
         </colorrole>
         <colorrole role="PlaceholderText">
          <brush brushstyle="SolidPattern">
           <color alpha="128">
            <red>170</red>
            <green>0</green>
            <blue>255</blue>
           </color>
          </brush>
         </colorrole>
        </inactive>
        <disabled>
         <colorrole role="Text">
          <brush brushstyle="SolidPattern">
           <color alpha="255">
            <red>120</red>
            <green>120</green>
            <blue>120</blue>
           </color>
          </brush>
         </colorrole>
         <colorrole role="PlaceholderText">
          <brush brushstyle="SolidPattern">
           <color alpha="128">
            <red>0</red>
            <green>0</green>
            <blue>0</blue>
           </color>
          </brush>
         </colorrole>
        </disabled>
       </palette>
      </property>
      <property name="currentIndex">
       <number>0</number>
      </property>
      <widget class="QWidget" name="tab">
       <attribute name="title">
        <string>File文件操作</string>
       </attribute>
       <layout class="QGridLayout" name="gridLayout_3">
        <item row="0" column="0">
         <widget class="QPlainTextEdit" name="plainTextEditIODivice">
          <property name="palette">
           <palette>
            <active>
             <colorrole role="Text">
              <brush brushstyle="SolidPattern">
               <color alpha="255">
                <red>0</red>
                <green>0</green>
                <blue>0</blue>
               </color>
              </brush>
             </colorrole>
             <colorrole role="PlaceholderText">
              <brush brushstyle="NoBrush">
               <color alpha="128">
                <red>0</red>
                <green>0</green>
                <blue>0</blue>
               </color>
              </brush>
             </colorrole>
            </active>
            <inactive>
             <colorrole role="Text">
              <brush brushstyle="SolidPattern">
               <color alpha="255">
                <red>0</red>
                <green>0</green>
                <blue>0</blue>
               </color>
              </brush>
             </colorrole>
             <colorrole role="PlaceholderText">
              <brush brushstyle="NoBrush">
               <color alpha="128">
                <red>0</red>
                <green>0</green>
                <blue>0</blue>
               </color>
              </brush>
             </colorrole>
            </inactive>
            <disabled>
             <colorrole role="Text">
              <brush brushstyle="SolidPattern">
               <color alpha="255">
                <red>120</red>
                <green>120</green>
                <blue>120</blue>
               </color>
              </brush>
             </colorrole>
             <colorrole role="PlaceholderText">
              <brush brushstyle="NoBrush">
               <color alpha="128">
                <red>0</red>
                <green>0</green>
                <blue>0</blue>
               </color>
              </brush>
             </colorrole>
            </disabled>
           </palette>
          </property>
         </widget>
        </item>
       </layout>
      </widget>
      <widget class="QWidget" name="tab_2">
       <attribute name="title">
        <string>textStream操作</string>
       </attribute>
       <layout class="QGridLayout" name="gridLayout">
        <item row="0" column="0">
         <widget class="QPlainTextEdit" name="plainTextEditTextStream"/>
        </item>
       </layout>
      </widget>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
  <widget class="QToolBar" name="toolBar">
   <property name="windowTitle">
    <string>toolBar</string>
   </property>
   <property name="toolButtonStyle">
    <enum>Qt::ToolButtonTextUnderIcon</enum>
   </property>
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
   <addaction name="actOpenFile"/>
   <addaction name="actSave"/>
   <addaction name="actOpenStream"/>
   <addaction name="actSaveStream"/>
   <addaction name="actExit"/>
  </widget>
  <action name="actOpenFile">
   <property name="icon">
    <iconset resource="res.qrc">
     <normaloff>:/images/icon/open.ico</normaloff>:/images/icon/open.ico</iconset>
   </property>
   <property name="text">
    <string>打开文件</string>
   </property>
   <property name="toolTip">
    <string>打开文件</string>
   </property>
   <property name="shortcut">
    <string>Ctrl+O</string>
   </property>
  </action>
  <action name="actSave">
   <property name="icon">
    <iconset resource="res.qrc">
     <normaloff>:/images/icon/save_as.ico</normaloff>:/images/icon/save_as.ico</iconset>
   </property>
   <property name="text">
    <string>另存为</string>
   </property>
   <property name="toolTip">
    <string>另存为</string>
   </property>
   <property name="shortcut">
    <string>Ctrl+Shift+S</string>
   </property>
  </action>
  <action name="actOpenStream">
   <property name="icon">
    <iconset resource="res.qrc">
     <normaloff>:/images/icon/add.ico</normaloff>:/images/icon/add.ico</iconset>
   </property>
   <property name="text">
    <string>流打开</string>
   </property>
   <property name="toolTip">
    <string>流打开</string>
   </property>
  </action>
  <action name="actSaveStream">
   <property name="icon">
    <iconset resource="res.qrc">
     <normaloff>:/images/icon/insert.ico</normaloff>:/images/icon/insert.ico</iconset>
   </property>
   <property name="text">
    <string>流另存为</string>
   </property>
   <property name="toolTip">
    <string>流另存为</string>
   </property>
  </action>
  <action name="actExit">
   <property name="icon">
    <iconset resource="res.qrc">
     <normaloff>:/images/icon/exit.ico</normaloff>:/images/icon/exit.ico</iconset>
   </property>
   <property name="text">
    <string>退出</string>
   </property>
   <property name="toolTip">
    <string>退出</string>
   </property>
  </action>
 </widget>
 <resources>
  <include location="res.qrc"/>
 </resources>
 <connections>
  <connection>
   <sender>actExit</sender>
   <signal>triggered()</signal>
   <receiver>MainWindow</receiver>
   <slot>close()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>-1</x>
     <y>-1</y>
    </hint>
    <hint type="destinationlabel">
     <x>381</x>
     <y>270</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_actOpenFile_triggered();
    void on_actSave_triggered();
    void on_actOpenStream_triggered();
    void on_actSaveStream_triggered();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QTextStream>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("打开文本文件");
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_actOpenFile_triggered()
{
    QString curPath = QCoreApplication::applicationDirPath();
    QString dilTile = "打开文件";
    QString filter = "程序(*.h *.cpp);;文本(*.txt);;所有文件(*.*)";
    QString filename = QFileDialog::getOpenFileName(this,dilTile,curPath,filter);

    if(filename.isEmpty()) return;

    QFile file(filename);
    if(file.open(QIODevice::ReadOnly|QIODevice::Text)){
        ui->plainTextEditIODivice->setPlainText(
                    QString::fromLocal8Bit(file.readAll()));
        file.close();
        ui->tabWidget->setCurrentIndex(0);
    }
}

void MainWindow::on_actSave_triggered()
{
    QString curPath = QCoreApplication::applicationDirPath();
    QString dilTile = "保存文件";
    QString filter = "程序(*.h *.cpp);;文本(*.txt);;所有文件(*.*)";
    QString filename = QFileDialog::getSaveFileName(this,dilTile,curPath,filter);

    if(filename.isEmpty()) return;

    QFile file(filename);
    if(file.open(QIODevice::WriteOnly|QIODevice::Text)){
        QString str = ui->plainTextEditIODivice->toPlainText();
        QByteArray strBytes = str.toUtf8();
        file.write(strBytes,strBytes.length());
        ui->tabWidget->setCurrentIndex(0);
    }
}

void MainWindow::on_actOpenStream_triggered()
{
    QString curPath = QCoreApplication::applicationDirPath();
    QString dilTile = "打开文件";
    QString filter = "程序(*.h *.cpp);;文本(*.txt);;所有文件(*.*)";
    QString filename = QFileDialog::getOpenFileName(this,dilTile,curPath,filter);

    if(filename.isEmpty()) return;

    QFile file(filename);
    if(file.open(QIODevice::ReadOnly|QIODevice::Text)){
        QTextStream txtstream(&file);
        txtstream.setAutoDetectUnicode(true);
        while (!txtstream.atEnd()) {
            ui->plainTextEditTextStream->appendPlainText(txtstream.readLine());
        }
        //ui->plainTextEditTextStream->setPlainText(txtstream.readAll());
        file.close();
        ui->tabWidget->setCurrentIndex(1);
    }
}

void MainWindow::on_actSaveStream_triggered()
{
    QString curPath = QCoreApplication::applicationDirPath();
    QString dilTile = "保存文件";
    QString filter = "程序(*.h *.cpp);;文本(*.txt);;所有文件(*.*)";
    QString filename = QFileDialog::getSaveFileName(this,dilTile,curPath,filter);

    if(filename.isEmpty()) return;

    QFile file(filename);
    if(file.open(QIODevice::WriteOnly|QIODevice::Text)){
        QTextStream txtstream(&file);
        txtstream.setAutoDetectUnicode(true);
        QString str = ui->plainTextEditTextStream->toPlainText();

        txtstream << str;
        ui->tabWidget->setCurrentIndex(1);
    }
}

二、效果演示

qt 保存文本文件,Qt/C++,qt,ui,开发语言文章来源地址https://www.toymoban.com/news/detail-627030.html

到了这里,关于Qt(八)文本文件(打开、修改与保存)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • vs打开qt的ui文件,报错闪退+已解决

    触发行为: 使用vs打开项目,点击ui文件后,数秒后qt designer闪退  报错内容: 方法一:鼠标右击 在qt designer 打开后,闪退前,鼠标右击 方法二:打开qt designer ,打开最近窗口 扩展-qt-qt vs tools-launch qt designer    方法三:自定义打开方式  鼠标右击ui文件,选择打开方式,选

    2024年02月15日
    浏览(35)
  • 【问题记录】Visual Studio 下的Qt工程无法打开 “xxx.ui“ 文件和LNK1104 无法打开文件“Qt5Cored.lib”错误

    Windows 11 家庭中文版 Microsoft Visual Studio Community 2022 (64 位) - Current 版本 17.5.3 qt-vsaddin-msvc2022-2.9.1-rev.06 VS下Qt环境准备好后,创建了Qt工程然后点击 Form Files 下的 “ xxx.ui ” 文件,在弹出  Qt 设计师 界面后闪退并显示如下错误 运行报错: LNK1104 无法打开文件“Qt5Cored.lib” 工具栏

    2024年02月05日
    浏览(35)
  • [编程语言][C++][Qt]单独添加UI文件

    不知什么原因,Qt Creator并不是很完美很智能。当先写好界面类的头文件和源代码文件后,我们再添加用于可视化界面设计的UI文件时,会出现一些问题。 当使用CMake管理项目时,CMake会读取 CMakeLists.txt 文件来确定各种项目设置。需要把 MainWindow.ui 包含进项目时,在 CMakeLists.

    2024年02月07日
    浏览(35)
  • VS2022+QT5.13.2:无法打开 源 文件 “ui_QtGuiApplicationx.h“在解决方案(亲测有效)

    @[TOC] 问题描述 VS2022+QT5.13.2 在工程文件,生成解决方案在时候提示如下错误: 错误(活动) E1696 无法打开 源 文件 “ui_QtWidgetsApplication1.h” QtWidgetsApplication1 E:testQtWidgetsApplication1QtWidgetsApplication1.h 4 错误 C1083 无法打开包括文件: “ui_QtWidgetsApplication1.h”: No such file or directory (编

    2024年02月05日
    浏览(36)
  • 【Qt】使用Qt designer(Qt设计师)以及uic工具将ui文件转换到C++代码进行界面开发

    【Qt】编写第一个Qt程序,使用Cmake编译并运行 Qt Designer是一个用于创建Qt应用程序用户界面的图形化界面设计器。它是Python GUI开发的一个重要工具,可以帮助用户快速创建具有丰富功能的用户界面。 打开终端输入以下代码 点击创建 拖一些组件进来,点击保存 得到一个后缀为

    2024年02月16日
    浏览(39)
  • Qt完成文本转换为语音播报与保存(系统内置语音引擎)(一)

    在当今数字化社会,人们对于交互式应用程序的需求越来越高。除了传统的图形用户界面,语音交互也成为了一种流行的交互方式。在这种情况下,将文本转换为语音成为了一项重要的技术,它可以为用户提供更加人性化和便捷的交互方式。在此背景下,Qt提供了QTextToSpeech类

    2024年02月15日
    浏览(33)
  • 07-1_Qt 5.9 C++开发指南_文件系统及文件读写_文本文件读写(使用 QTextStream 进行文件读写更为方便)

    文本文件是指以纯文本格式存储的文件,例如用 Qt Creator 编写的 C++程序的头文件 (.h 文件)和源程序文件 (.cpp 文件)。HTML 和 XML 文件也是纯文本文件,只是其读取之后需要对内容进行解析之后再显示。 Qt 提供了两种读写纯文本文件的基本方法, 一种是用 QFile 类的 IODevice 读写

    2024年02月13日
    浏览(42)
  • vs+qt开发中,报错找不到ui_QtWidgetsApplication1.h文件

    添加包含目录和库目录以后如果还是不行,我自己发现了把Qt project Settings里show in build log改成是就好了。 但是这样以后每次修改ui的时候需要打开属性把Qt project Settings里show in build log改成否然后应用一下再改成是,你添加的控件又可以识别到了。

    2024年01月18日
    浏览(53)
  • Qt/C++音视频开发69-保存监控pcm音频数据到mp4文件/监控录像/录像存储和回放/264/265/aac/pcm等

    用ffmpeg做音视频保存到mp4文件,都会遇到一个问题,尤其是在视频监控行业,就是监控摄像头设置的音频是PCM/G711A/G711U,解码后对应的格式是pcm_s16be/pcm_alaw/pcm_mulaw,将这个原始的音频流保存到mp4文件是会报错的,在调用avformat_write_header写文件头的时候提示(-22) Invalid argument,

    2024年04月11日
    浏览(41)
  • C++ Qt框架开发 | 基于Qt框架开发实时成绩显示排序系统(3) 保存表格数据

    对上两篇篇的工作C++ Qt框架开发| 基于Qt框架开发实时成绩显示排序系统(1)-CSDN博客和C++ Qt框架开发 | 基于Qt框架开发实时成绩显示排序系统(2)折线图显示-CSDN博客继续优化,增加一个 保存按钮,用于保存成绩数据。 1)在ui界面添加一个按钮         将其命名为saveBtn。

    2024年02月19日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包