1、效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e0366912669b62306ecd3ab176d07052.png)
2、代码
2.1mainwidow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <subtext.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void doProcessTriggeredByNew(bool);
void doProcessTriggeredByOpen(bool);
void doProcessTriggeredByUTF_8(bool);
void doProcessTriggeredByGB2312(bool);
private:
Ui::MainWindow *ui;
QString codeName;
void Init();
};
#endif
2.2 subtext.h
#ifndef SUBTEXT_H
#define SUBTEXT_H
#include <QWidget>
#include <QTextEdit>
#include <QFileDialog>
#include <QFile>
#include <QMessageBox>
#include <QTextStream>
class subText : public QTextEdit
{
Q_OBJECT
public:
explicit subText(QWidget *parent = nullptr);
void NewFile();
void OpenFile();
void setCodeName(const QString codename);
signals:
public slots:
void doProcesscontentsChanged();
private:
QString filename;
QFile *myfile;
QString CodeName;
};
#endif
2.3 mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QFont font;
font.setBold(true);
font.setStyle(QFont::StyleOblique);
ui->action_New->setFont(font);
Init();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::Init(){
codeName = "UTF-8";
connect(ui->action_New,SIGNAL(triggered(bool)),
this,SLOT(doProcessTriggeredByNew(bool)));
connect(ui->action_Open,SIGNAL(triggered(bool)),
this,SLOT(doProcessTriggeredByOpen(bool)));
connect(ui->actionUTF8,SIGNAL(triggered(bool)),
this,SLOT(doProcessTriggeredByUTF_8(bool)));
connect(ui->actionUTF8,SIGNAL(triggered(bool)),
this,SLOT(doProcessTriggeredByGB2312(bool)));
}
void MainWindow::doProcessTriggeredByNew(bool){
qDebug()<<"triggered New action"<<endl;
subText *sub = new subText();
ui->mdiArea->addSubWindow((QWidget *)sub);
sub->setCodeName(codeName);
sub->NewFile();
sub->show();
}
void MainWindow::doProcessTriggeredByOpen(bool){
qDebug()<<"triggered open action"<<endl;
subText *sub = new subText();
ui->mdiArea->addSubWindow((QWidget *)sub);
sub->setCodeName(codeName);
sub->OpenFile();
sub->show();
}
void MainWindow::doProcessTriggeredByUTF_8(bool){
qDebug()<<"triggered UTF-8 action"<<endl;
this->codeName = "UTF-8";
}
void MainWindow::doProcessTriggeredByGB2312(bool){
qDebug()<<"triggered GB18030 action"<<endl;
this->codeName = "GB18030";
}
2.4 subtext.cpp
#include "subtext.h"
subText::subText(QWidget *parent) : QTextEdit(parent)
{
myfile = new QFile(this);
}
void subText::NewFile(){
static int index=1;
QString title =QString("未命名文档[%1][*]").arg(index);
this->setWindowTitle(title);
index++;
connect(this->document(),SIGNAL(contentsChanged()),this,SLOT(doProcesscontentsChanged()));
}
void subText::OpenFile(){
QString filename = QFileDialog::getOpenFileName(this,"获取文本文件",".","Text(*.cpp *.txt *.h)");
if(filename.isEmpty()){
return;
}
this->filename = filename;
QFileInfo info(filename);
QString title = info.fileName();
this->setWindowTitle(title);
myfile->setFileName(filename);
bool ret=myfile->open(QIODevice::ReadOnly|QIODevice::ReadOnly);
if(!ret){
QMessageBox::warning(this,"打开文件失败","打开文件失败");
}
QTextStream stream(myfile);
stream.setCodec(this->CodeName.toLocal8Bit().data());
while(!stream.atEnd()){
QString str=stream.readLine();
this->append(str);
myfile->close();
}
}
void subText::setCodeName(const QString codename){
this->CodeName = codename;
}
void subText::doProcesscontentsChanged(){
this->setWindowModified(true);
}