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

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