Qt學習筆記(三)---製作一個記事本

mainwindow.h

#include <QFileDialog>
#include <QTextStream>
#include <QFontDialog>
#include <QFont>
#include <QColor>
#include <QColorDialog>
#include <QTextCodec>
#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 printFileSlot();
    void setFontSlot();//設置字體的槽
    void setColorSlot();//設置文本顏色的槽
    void currentDateTimeSlot();//獲取當前的系統時間,並採用一定的格式顯示出來
};

#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);
    //QObject::connect()
    this->setWindowTitle("NotePad");
    QObject::connect(ui->action_N,SIGNAL(triggered()),this,SLOT(newFileSlot()));
    QObject::connect(ui->action_O,SIGNAL(triggered()),this,SLOT(openFileSlot()));
    QObject::connect(ui->action_S,SIGNAL(triggered()),this,SLOT(saveFileSlot()));
    QObject::connect(ui->action_A,SIGNAL(triggered()),this,SLOT(saveAsFileSlot()));
    QObject::connect(ui->action_X,SIGNAL(triggered()),this,SLOT(close()));
    //edit menu
    QObject::connect(ui->undoAction,SIGNAL(triggered()),ui->textEdit,SLOT(undo()));
    QObject::connect(ui->redoAction,SIGNAL(triggered()),ui->textEdit,SLOT(redo()));
    QObject::connect(ui->cutAction,SIGNAL(triggered()),ui->textEdit,SLOT(cut()));
    QObject::connect(ui->copyAction,SIGNAL(triggered()),ui->textEdit,SLOT(copy()));
    QObject::connect(ui->pasteAction,SIGNAL(triggered()),ui->textEdit,SLOT(paste()));
    QObject::connect(ui->selectAllAction,SIGNAL(triggered()),ui->textEdit,SLOT(selectAll()));

    //字體顏色
    QObject::connect(ui->colorAction,SIGNAL(triggered()),this,SLOT(setColorSlot()));
    QObject::connect(ui->fontAction,SIGNAL(triggered()),this,SLOT(setFontSlot()));
    //顯示時間
    QObject::connect(ui->datetimeAction,SIGNAL(triggered()),this,SLOT(currentDateTimeSlot()));

}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::newFileSlot()
{
    //如果當前文檔的內容已經改變了 Modified:改變
    if(ui->textEdit->document()->isModified())
    {
        qDebug()<<"current file modified";
    }
    else
    {
        //qDebug()<<"not modified";
        ui->textEdit->clear();
        this->setWindowTitle("Not Modified");
    }
}


void MainWindow::openFileSlot()
{
    //get the file name
    QString fileName=QFileDialog::getOpenFileName(this,"Open File",QDir::currentPath());
    //qDebug()<<"fileName is"<<fileName;
    if(fileName.isEmpty())//如果爲空,
    {
        QMessageBox::information(this,"Error Message","Please Select a Text File");
        return;
    }
    QFile *file=new QFile;
    file->setFileName(fileName);//設置文件名稱
   // 以讀取單一模式打開文件
    bool ok=file->open(QIODevice::ReadOnly);

    if(ok)
    {
        //文件與文本流相關聯
        QTextStream in(file);
        ui->textEdit->setText(in.readAll());//從file中讀出所有的文件內容
        file->close();
        delete file;
    }
    else
    {
        QMessageBox::information(this,"Error Message","File Open Error"+file->errorString());
        return;
    }
}

void MainWindow::saveFileSlot()
{
   //saveFileName是否爲空,空執行另存爲操作,
    if(saveFileName.isEmpty())
   {
       this->saveAsFileSlot();
   }
   else
   {
        //非空,進行保存操作
       QFile *file=new QFile;//創建文件對象
       file->setFileName(saveFileName);//設置文件名稱爲fileName
       bool ok=file->open(QIODevice::WriteOnly);
       if(ok)
       {
           QTextStream out(file);
           out<<ui->textEdit->toPlainText();//這裏是去除textEdit當中的純文本
           file->close();
           this->setWindowTitle(saveFileName+"-----notepad");
           delete file;
       }

   }
}
void MainWindow:: saveAsFileSlot()
{
    //getSaveFileName
    QString saveFileName=QFileDialog::getSaveFileName(this,"Save File",QDir::currentPath());

    if(saveFileName.isEmpty())
    {
        QMessageBox::information(this,"Error Message","Please Select A File");
        return;

    }
    QFile *file=new QFile;//創建文件對象
    file->setFileName(saveFileName);//設置文件名稱爲fileName
    bool ok=file->open(QIODevice::WriteOnly);
    if(ok)
    {
        QTextStream out(file);
        out<<ui->textEdit->toPlainText();//這裏是去除textEdit當中的純文本
        file->close();
        this->setWindowTitle(saveFileName+"-----notepad");
        delete file;
    }
    else
    {
        QMessageBox::information(this,"Error Message","Save File Error");
        return;
    }
}

void MainWindow::setFontSlot()
{
    //獲得用戶選擇的字體

    /*
   bool ok;
   QFont font = QFontDialog::getFont(
                    &ok, QFont("Helvetica [Cronyx]", 10), this);
    if (ok) {
        // the user clicked OK and font is set to the font the user selected
    } else {
        // the user canceled the dialog; font is set to the initial
        // value, in this case Helvetica [Cronyx], 10
    }
    */

    bool ok;
    QFont font=QFontDialog::getFont(&ok,this);
    if(ok)
    {
        ui->textEdit->setFont(font);
    }
    else
    {
        QMessageBox::information(this,"Error Message","Error Set Font");
        return;

    }
}

void MainWindow::setColorSlot()
{
    /*
      const QColorDialog::ColorDialogOptions options = QFlag(colorDialogOptionsWidget->value());
      const QColor color = QColorDialog::getColor(Qt::green, this, "Select Color", options);

      if (color.isValid()) {
          colorLabel->setText(color.name());
          colorLabel->setPalette(QPalette(color));
          colorLabel->setAutoFillBackground(true);
      }
     */
     QColor color=QColorDialog::getColor(Qt::green,this);
     if(color.isValid())
     {
         ui->textEdit->setTextColor(color);
     }
     else
     {
         QMessageBox::information(this,"Error Message","Color is unvalid");
         return;
     }
}


void MainWindow::currentDateTimeSlot()
{
   QDateTime current=QDateTime::currentDateTime();
   QString time=current.toString("yyyy-M-dd hh:mm:ss");
   ui->textEdit->append(time);
}
界面文件:




發佈了56 篇原創文章 · 獲贊 72 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章