QT編寫一個簡單的記事本(notepad部分功能實現)代碼主要來自視頻(丁林松--QT高級編程視頻教程-最強初級到高級編程開發)

程序效果

 

代碼:

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QtDebug>
#include<QFile>
#include<QFileDialog>
#include<QDir>
#include<QTextStream>
#include<QMessageBox>
#include<QFont>
#include<QFontDialog>
#include<QColor>
#include<QColorDialog>
#include<QDateTime>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QString saveFileName;

private slots:
    void newFileSlot();
    void openFileSlot();
    void saveFileSlot();
    void saveAsFileSlot();
    void setFontSlot();
    void setColorSlot();
    void currentDateSlot();
};

#endif // MAINWINDOW_H

 

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("unnamed notepad");
    QObject::connect(ui->newAction,SIGNAL(triggered(bool)),this,SLOT(newFileSlot()));
    QObject::connect(ui->openAction,SIGNAL(triggered(bool)),this,SLOT(openFileSlot()));
    QObject::connect(ui->saveAction,SIGNAL(triggered(bool)),this,SLOT(saveFileSlot()));
    QObject::connect(ui->saveAsAction,SIGNAL(triggered(bool)),this,SLOT(saveAsFileSlot()));
    QObject::connect(ui->fontAction,SIGNAL(triggered(bool)),this,SLOT(setFontSlot()));
    QObject::connect(ui->colorAction,SIGNAL(triggered(bool)),this,SLOT(setColorSlot()));
    QObject::connect(ui->datetimeAcrtion,SIGNAL(triggered(bool)),this,SLOT(currentDateSlot()));

    QObject::connect(ui->undoAction,SIGNAL(triggered(bool)),ui->textEdit,SLOT(undo()));
    QObject::connect(ui->redoAction,SIGNAL(triggered(bool)),ui->textEdit,SLOT(redo()));
    QObject::connect(ui->copyAction,SIGNAL(triggered(bool)),ui->textEdit,SLOT(copy()));
    QObject::connect(ui->pasteAction,SIGNAL(triggered(bool)),ui->textEdit,SLOT(paste()));
    QObject::connect(ui->selectAllAction,SIGNAL(triggered(bool)),ui->textEdit,SLOT(selectAll()));
    QObject::connect(ui->cutAction,SIGNAL(triggered(bool)),ui->textEdit,SLOT(cut()));


}

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

void MainWindow::newFileSlot(){
    if(ui->textEdit->document()->isModified()){
        qDebug()<<"I have changed";
    }else{
        qDebug()<<"not me";
        this->setWindowTitle("Failed");
    }
}

void MainWindow::openFileSlot(){
    QString mystring=QFileDialog::getOpenFileName(this,"open file",QDir::currentPath());
    if(mystring.isEmpty()){
        QMessageBox::warning(this,"warning","didn't find file");
        return;
    }
    qDebug()<<"file name is"<<mystring;
    QFile *myfile=new QFile;
    myfile->setFileName(mystring);
    bool ok=myfile->open(QIODevice::ReadOnly);
    if(ok){
        QTextStream in(myfile);
        ui->textEdit->setText(in.readAll());
        QMessageBox::information(this,"good job","file will open");
        myfile->close();
        delete myfile;
    }else{
        QMessageBox::warning(this,"Error","file can't open");
    }
}

void MainWindow::saveFileSlot(){
    if(saveFileName.isEmpty()){
        this->saveAsFileSlot();
    }else{
        QFile *myfile=new QFile;
        myfile->setFileName(saveFileName);
        bool ok=myfile->open(QIODevice::WriteOnly);
        if(ok){
            QTextStream out(myfile);
            out<<ui->textEdit->toPlainText();
            myfile->close();
            delete myfile;
        }else{
            QMessageBox::warning(this,"warning in save","error"+myfile->errorString());
        }
    }
}

void MainWindow::saveAsFileSlot(){
    saveFileName=QFileDialog::getSaveFileName(this,"save file",QDir::currentPath());
    if(saveFileName.isEmpty()){
        QMessageBox::warning(this,"warning","there's some error");
    }
    qDebug()<<"file is"<<saveFileName;
    QFile *myfile=new QFile;
    myfile->setFileName(saveFileName);
    bool ok=myfile->open(QIODevice::WriteOnly);
    if(ok){
        QTextStream out(myfile);
        out<<ui->textEdit->toPlainText();
        myfile->close();
        delete myfile;
    }else{
        QMessageBox::warning(this,"warning in saveas","error"+myfile->errorString());
    }
}


void MainWindow::setFontSlot(){
    bool ok;
    QFont font=QFontDialog::getFont(&ok,this);
    if(ok){
        ui->textEdit->setFont(font);
    }

}

void MainWindow::setColorSlot(){
    QColor color=QColorDialog::getColor(Qt::red,this);
    if(color.isValid()){
        ui->textEdit->setTextColor(color);
    }

}

void MainWindow::currentDateSlot(){
    QDateTime time=QDateTime::currentDateTime();
    QString string=time.toString("yyyy_M_dd hh_mm_ss");
    ui->textEdit->append(string);
}


 

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