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);
    }
}

二、效果演示

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/wsp_1138886114/article/details/128094551
今日推荐