Qt学习笔记(三)---制作一个记事本

这篇博客详细记录了使用Qt框架开发一个简单记事本应用的过程,涵盖了mainwindow.h和mainwindow.cpp两个关键文件的代码实现,是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);
}
界面文件:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值