C++课程设计——Horizon多开记事本

此课程设计是基于Qt软件平台提供的第三方库所写的软件,故只能在Qt上运行。

  1. 主函数代码如下:
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
  1. 记事本主窗口代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QMdiSubWindow>
#include <QWidget>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("Horizon记事本");
    this->setWindowIcon(QIcon("./logo.ico"));
}

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

subText *MainWindow::getActiveChildForm(){ //得到选中窗体中的文本
    subText *sub = nullptr;
    QMdiSubWindow *subWin = ui->mdiArea->activeSubWindow();
    if(subWin == nullptr) return sub;
    QWidget *wid = subWin->widget();
    if(wid == nullptr) return sub;
    sub = (subText *)wid;
    return sub;
}

void MainWindow::on_action_new_triggered()//新建文件
{
    subText *sub = new subText();
    ui->mdiArea->addSubWindow(sub);
    sub->NewFile();
    sub->show();
}

void MainWindow::on_action_open_triggered()//打开文件
{
    subText *sub = new subText();
    ui->mdiArea->addSubWindow(sub);
    int ret = sub->OpenFile();
    if(ret == -1){
        return;
    }
    if(ret == 0){
        sub->show();
    }
}

void MainWindow::on_action_save_triggered()//保存
{
    QMdiSubWindow *subWin = ui->mdiArea->activeSubWindow();
    QWidget *wid =  subWin->widget();
    subText *sub = (subText *)wid;
    sub->SaveFile();
}

void MainWindow::on_action_saveAs_triggered()//另存为
{
    QMdiSubWindow *subWin = ui->mdiArea->activeSubWindow();
    subText *sub = (subText *)subWin->widget();
    sub->SaveAsFile();
}

void MainWindow::on_action_exit_triggered()//退出程序
{
    close();
}

void MainWindow::on_action_close_triggered()//关闭当前页面
{
    ui->mdiArea->closeActiveSubWindow();
}

void MainWindow::on_action_closeAll_triggered()//关闭所有子页面
{
    ui->mdiArea->closeAllSubWindows();
}

void MainWindow::on_action_tile_triggered()//平铺子页面
{
     ui->mdiArea->tileSubWindows();
}

void MainWindow::on_action_cascade_triggered()//重叠子页面
{
     ui->mdiArea->cascadeSubWindows();
}

void MainWindow::on_action_next_triggered()//下一个
{
     ui->mdiArea->activateNextSubWindow();
}

void MainWindow::on_action_prevous_triggered()//上一个
{
     ui->mdiArea->activatePreviousSubWindow();
}

void MainWindow::on_action_design_triggered()//课设要求
{
   QMessageBox::about(this,"课设要求","第一类,基于Windows界面应用程序界面制作类\n要求:\n界面逼真,功能相似(只需演示功能,不一定具有真正的功能)。软件界面模仿类,比如:仿VC++6.0界面游戏界面模仿类、仿联众游戏、qq游戏等界面其他界面、仿Windows Media Player9.0界面等");
}

void MainWindow::on_action_message_triggered()//课设信息
{
    QMessageBox::about(this,"小组信息","专业班级:计算机科学与技术 1802班\n组号:第 组\n课题名:多开记事本(horizon)\n小组成员: \n\t   ");
}

void MainWindow::on_action_redo_triggered()//撤销
{
    subText *sub = getActiveChildForm();
    if(sub != nullptr){
        sub->redo();
    }
}

void MainWindow::on_action_copy_triggered()//复制
{
    subText *sub = getActiveChildForm();
    if(sub != nullptr){
        sub->copy();
    }
}

void MainWindow::on_action_cut_triggered()//剪切
{
    subText *sub = getActiveChildForm();
    if(sub != nullptr){
        sub->cut();
    }
}

void MainWindow::on_action_undo_triggered()//恢复
{
    subText *sub = getActiveChildForm();
    if(sub != nullptr){
        sub->undo();
    }
}

void MainWindow::on_action_paste_triggered()//粘贴
{
    subText *sub = getActiveChildForm();
    if(sub != nullptr){
        sub->paste();
    }
}

  1. 记事本子窗口代码:
#include "subtext.h"
#include <QString>
subText::subText(QWidget *parent) : QTextEdit(parent)
{
    isEdit = false;
    this->filename.clear();
    myFile = new QFile();
}

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

int subText::OpenFile(){
    // 获取要打开的文本文件
    QString filename = QFileDialog::getOpenFileName(this,"获取文件","../../","Text(*.cpp *.h *.txt)");
       if(filename.isEmpty()){
           return -1;
       }
       //将文件路径保存,设置文件名
       this->filename = filename;
       QFileInfo info(filename);
       this->setWindowTitle(info.fileName()+"[*]");
       //打开文件
       myFile->setFileName(filename);
       bool ret = myFile->open(QIODevice::ReadOnly|QIODevice::ReadOnly);
       if(!ret){
           QMessageBox::warning(this,"打开失败","打开文件失败,请确保文件存在或未损坏:-)");
           return -1;
       }
       QTextStream stream(myFile);
       stream.setCodec("UTF-8");
       while(!stream.atEnd()){
           this->append(stream.readLine());
       }
       myFile->close();
       connect(this->document(),SIGNAL(contentsChanged()),this, SLOT(doProcessContentsChange()));//判断编辑状态的连接
       return 0;
}



void subText::SaveFile(){
    if(this->filename.isEmpty()){//判断是新建还是打开
        QString filename =  QFileDialog::getSaveFileName(this,"保存",".","Text(*.cpp *.h *.txt)");
        if(filename.isEmpty()){
            return;
        }
        this->filename = filename;
    }

        myFile->setFileName(this->filename);//文件写入操作
        bool ret = myFile->open(QIODevice::WriteOnly|QIODevice::Text);
        if(!ret){
            QMessageBox::warning(this,"失败","文件已不存在或保存地址出现问题");
            return;
        }
        QTextStream stream(myFile);
        stream.setCodec("UTF-8");
        stream << this->toPlainText();
        stream.flush();
        myFile->close();
        this->setWindowModified(false);
        isEdit = false;
        QMessageBox::information(this,"成功提示","保存文件成功:-)");
}

void subText::SaveAsFile(){
    QString filename =  QFileDialog::getSaveFileName(this,"保存",".","Text(*.cpp *.h *.txt)");
        if(filename.isEmpty()){
            return;
        }
        this->filename = filename;
        myFile->setFileName(this->filename);//文件写入操作
        bool ret = myFile->open(QIODevice::WriteOnly|QIODevice::Text);
        if(!ret){
            QMessageBox::warning(this,"失败","文件已不存在或保存地址出现问题");
            return;
        }
        QTextStream stream(myFile);
        stream.setCodec("UTF-8");
        stream << this->toPlainText();
        stream.flush();
        myFile->close();
        this->setWindowModified(false);
        isEdit = false;
        QMessageBox::information(this,"成功提示","保存文件成功:-)");
}

void subText::doProcessContentsChanged(){
    isEdit = true;
    this->setWindowModified(true);
}

void subText::closeEvent(QCloseEvent *event){
    if(!isEdit){
        return;
    }else{
        QMessageBox::StandardButton ret = QMessageBox::information(this,"保存","关闭前您确定要保存嘛?",QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
        if(ret == QMessageBox::Yes){
          SaveFile();
          event->accept();
        }
        if (ret ==QMessageBox::No) {
          event->accept();
        }
        if(ret ==QMessageBox::Cancel){
          event->ignore();
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章