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

    xiaoxiao2021-03-25  93

    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); }界面文件:

    转载请注明原文地址: https://ju.6miu.com/read-34820.html

    最新回复(0)