QT編寫一個簡單的記事本

  1,ui界面界面設計,在最右面修改類和對象,對象的名稱 。關聯部件和槽函數

#ifndef MAINWINDOW_H
#define MAINWINDOW_H


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

#include<QUrl>
#include<QDesktopServices>
//命名空間Ui中聲明瞭一個MainWindow類
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow//類是共有繼承QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);//explicit:不能發生相應的隱式類型轉換,parent=0表示這個部件沒有祖先,也就是說這個部件不是嵌套在另一個部件中的。
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QString saveFileName;
private slots:
    void newFileSlot();
    void openFileSlot();
    void saveFileSlot();
    void saveAsFileSlot();
    void setFontSlot();
    void setColorSlot();
   void currentDataSlot();
   void  aboutWebsiteSlot();
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)//創建了一個ui對象,也就是用戶接口。ui接口中包括一些可見的部件如按鈕,光標,文本框等。
{
      ui->setupUi(this);
      this->setWindowTitle("unnamed notepad");

     QObject::connect(ui->newAction,SIGNAL(triggered(bool)),this,SLOT(newFileSlot()));//關聯信號和槽 triggered():點擊
     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(currentDataSlot()));

      //textEdit 自己實現了槽函數方法,直接調用。
      QObject::connect(ui->undoAction,SIGNAL(triggered(bool)),ui->textEdit,SLOT(undo()));
      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()));

       QObject::connect(ui->aboutQtAction,SIGNAL(triggered(bool)),qApp,SLOT(aboutQt()));
       //提問:qApp到底是什麼?
           //回答:QApplication::instance();應用程序實例化全局指針
           //這個槽函數直接調用Qt相關文檔,不用自己實現
       QObject::connect(ui->aboutWebAction,SIGNAL(triggered(bool)),this,SLOT(aboutWebsiteSlot));
}

void MainWindow::newFileSlot()
{
    if(ui->textEdit->document()->isModified()){  //文本被改變
        qDebug()<<"I Have change";    
    }else{
        qDebug()<<"not me";
         this->setWindowTitle("failed");
         this->setWindowTitle("untitled-------------------------");
    }
}
void MainWindow::openFileSlot()
{
    // QString mystring=QFileDialog::getOpenFileName
    //(this,"open file",QDir::currentPath());
    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 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();  //
          //            QPlainTextEdit 多行簡單文本框用 toPlainText();[pleɪn]
         //            QTextEdit 富文本框,簡單文本用toPlainText(),  富文本用 toHtml().
        //            QLineEdit 用 text().  獲取文本信息。
            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;      字體(QFontDialog) 》example
      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!!!");
    }
}
void MainWindow::setColorSlot(){
    /*QColorDialog:》  Standard Dialogs Example
     * 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::red,this);
        if(color.isValid()){
            ui->textEdit->setTextColor(color);
        }
}
void MainWindow::currentDataSlot(){
    /*
QTime time1 = QTime::fromString("131", "HHh");
  // time1 is 13:00:00
  QTime time1 = QTime::fromString("1apA", "1amAM");
  // time1 is 01:00:00

  QDateTime dateTime2 = QDateTime::fromString("M1d1y9800:01:02",
                                              "'M'M'd'd'y'yyhh:mm:ss");
  // dateTime is 1 January 1998 00:01:02
*/

    QDateTime time=QDateTime::currentDateTime();
    QString string=time.toString("yyyy_M_dd hh_mm_ss");//2019_8_22  21_34_14
    ui->textEdit->append(string);
}

void MainWindow::aboutWebsiteSlot()
{
    qDebug()<<"not me";
    QDesktopServices::openUrl(QUrl("https://blog.csdn.net/qq_31339017/article/category/7664554"));

}



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

 

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