.pro
#------------------------------------------------- # # Project created by QtCreator 2024-01-09T09:28:20 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = myNotePad TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which has been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui
.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include<QMessageBox> #include<QFile> #include<QTextStream>// QTextStream是一个非常强大的类,它可以对IO设备,QString,QByteArray等类进行方便的读写操作 #include<QFileDialog>//QFileDialog 是Qt 中用于文件打开和保存的对话框 #include<QKeyEvent> //键盘事件头文件 #include<QMouseEvent> //鼠标事件头文件 #include<QDebug> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void keyPressEvent(QKeyEvent *k); //声明键盘按下事件 void mousePressEvent(QMouseEvent *m); //声明鼠标按下事件 QFile file; private: Ui::MainWindow *ui; private slots: void newActionSlot(); void openActionSlot(); void saveActionSlot(); void on_saveButton_clicked(); void on_openButton_clicked(); void on_saverightButton_clicked(); }; #endif // MAINWINDOW_H
main.cpp#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->newAction,&QAction::triggered,this,&MainWindow::newActionSlot); connect(ui->openAction,&QAction::triggered,this,&MainWindow::openActionSlot); connect(ui->saveAction,&QAction::triggered,this,&MainWindow::saveActionSlot); } MainWindow::~MainWindow() { delete ui; } void MainWindow::newActionSlot() { ui->textEdit->clear(); this->setWindowTitle("新建文本文档"); } void MainWindow::openActionSlot() { QString fileName = QFileDialog::getOpenFileName(this, "選擇一個文件",QCoreApplication::applicationFilePath(), "*.cpp "); //(父類對象,表頭名稱,获取当前路径,),跳出个寻找窗口来寻找文件 if(fileName.isEmpty()) //判空 { QMessageBox::warning(this,"警告","请选择一个文件"); //弹对话框 } else //不空,执行 { QFile file(fileName); //创建文件对象 file.open(QIODevice::ReadOnly); //打开文件的内容 QByteArray ba = file.readAll(); //读取文件的内容 ui->textEdit->setText(QString(ba)); //在ui界面的textEdit中显示文字刚才读取的baQByteArray file.close(); } } void MainWindow::saveActionSlot() { QString fileName = QFileDialog::getSaveFileName(this,"请选择一个文件", //读取该文件路径的文件, 跳出个对话窗口 QCoreApplication::applicationFilePath()); if(fileName.isEmpty()) //判空 { QMessageBox::warning(this,"警告","请选择一个文件"); } else //不空,执行 { QFile file(fileName); //声明个file存储这个文件路径的文件 file.open(QIODevice::WriteOnly); //打开文件-以只读的方式 --一定要打开文件一下,否则其是以Notopen形式打开的,是无法读写的 //ui->textEdit->toPlainText(); //读取ui界面textEdit控件的内容,生成了一个 QByteArray ba; //定义一个ba变量 ba.append(ui->textEdit->toPlainText()); //把QString接在QByteArray后边 file.write(ba); //保存该文件 file.close(); //关闭该文件。 } } void MainWindow::on_openButton_clicked() { QString fileName =QFileDialog::getOpenFileName(this,"要打开的位置",QCoreApplication::applicationFilePath(),"*.txt"); if(fileName.isEmpty()) { QMessageBox::warning(this,"警告","打开失败"); } else { QFile file(fileName); file.open(QIODevice::ReadOnly); QByteArray ba = file.readAll(); ui->textEdit_2->setText(QString(ba)); file.write(ba); file.close(); } } void MainWindow::on_saveButton_clicked() { QString fileName = QFileDialog::getSaveFileName(this,"要保存到哪呢",QCoreApplication::applicationFilePath()); if(fileName.isEmpty()) { QMessageBox::warning(this,"警告","保存失败"); } else { QFile file(fileName); file.open(QIODevice::WriteOnly); //打开文件-以只读的方式 QByteArray ba; ba.append(ui->textEdit_2->toPlainText()); file.write(ba); file.close(); } } void MainWindow::on_saverightButton_clicked() { QString saveFilePath = "D:\\QTProject\\123.txt"; // 设置要保存的文件路径 QFile file(saveFilePath); if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream out(&file); out << ui->textEdit_2->toPlainText(); file.close(); QMessageBox::information(this,"提示","保存成功"); } else { QMessageBox::warning(this, "警告", "无法保存文件"); } } void MainWindow::keyPressEvent(QKeyEvent *k) { if(k->modifiers()==Qt::ControlModifier&&k->key()==Qt::Key_S) //modifiers是ctrl、alt等的按键函数调用, key()是获取普通按键的函数调用。 { saveActionSlot();//调用窗口1的保存任务 } } void MainWindow::mousePressEvent(QMouseEvent *m) { QPoint pt = m->pos();//获取鼠标坐标 qDebug() << pt; //打印坐标 if(m->button()==Qt::LeftButton) //鼠标操作 { qDebug() << "左键被按下"; } else if(m->button()== Qt::RightButton) { qDebug() << "右键被按下"; } }