区别于之前创建爱的各个工程,这次我们在这里选择基类为QMainWindow。
然后默认目录就是
直接到对应文件中进行代码的书写:
main.cpp:
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.resize(800,500); w.show(); return a.exec(); }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTextEdit> #include <QMenuBar> #include <QMenu> #include <QFileDialog> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); private: bool isChanged; QString fileNameString;//文件名字 QTextEdit *text1;//这里是一个指针不要忘记了。总是在这个地方出现问题。 QMenu *fileMenu;//文件下拉菜单 QAction *newFile;//新建文件。 QAction *openFile;//打开文件。 QAction *saveFile;//保存文件。 QAction *quitFile;//推出。 QMenu *editMenu;//编辑菜单 QAction *copyEdit; QAction *pasteEdit; QAction *cutEdit; QAction *allSelectEdit; QMenu *helpMenu;//帮助菜单 QAction *aboutSoftware; QAction *howToUse; private slots: void on_new(); void on_open(); void on_save(); void on_quit(); void on_copy(); void on_paste(); void on_cut(); void on_allSelect(); void on_howToUse(); void on_aboutSoftware(); void on_changed(); }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include <QMessageBox> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { isChanged = false; text1 = new QTextEdit;//中央编辑区域的实例化 this->setCentralWidget(text1);//设置到中央编辑页面。 text1->setFontPointSize(16);//修改字体大小。 //添加文件下拉菜单 fileMenu = this->menuBar()->addMenu("文件"); //为下拉菜单添加文件项: newFile = new QAction("新建",this);//实例化 newFile->setShortcut(tr("CTRL+N")); fileMenu->addAction(newFile);//添加到显示 connect(newFile,SIGNAL(triggered()),this,SLOT(on_new()));//为当前这个文件项添加一个出发事件,当前窗体响应,执行函数为on_new(). openFile = new QAction("打开",this);//实例化 openFile->setShortcut(tr("CTRL+O")); fileMenu->addAction(openFile); connect(openFile,SIGNAL(triggered()),this,SLOT(on_open())); saveFile = new QAction("保存",this); saveFile->setShortcut(tr("CTRL+S")); fileMenu->addAction(saveFile); connect(saveFile,SIGNAL(triggered()),this,SLOT(on_save())); quitFile = new QAction("退出",this); quitFile->setShortcut(tr("CTRL+Q")); fileMenu->addAction(quitFile); connect(quitFile,SIGNAL(triggered()),this,SLOT(on_quit())); editMenu =this->menuBar()->addMenu("编辑"); copyEdit = new QAction("复制",this); copyEdit->setShortcut(tr("CTRL+C")); editMenu->addAction(copyEdit); connect(copyEdit,SIGNAL(triggered()),this,SLOT(on_copy())); pasteEdit = new QAction("粘贴",this); pasteEdit->setShortcut(tr("CTRL+V")); editMenu->addAction(pasteEdit); connect(pasteEdit,SIGNAL(triggered()),this,SLOT(on_paste())); cutEdit = new QAction("剪切",this); cutEdit->setShortcut(tr("CTRL+X")); editMenu->addAction(cutEdit); connect(cutEdit,SIGNAL(triggered()),this,SLOT(on_cut())); allSelectEdit = new QAction("全选",this); allSelectEdit->setShortcut(tr("CTRL+A")); editMenu->addAction(allSelectEdit); connect(allSelectEdit,SIGNAL(triggered()),this,SLOT(on_allSelect())); helpMenu = this->menuBar()->addMenu("帮助"); aboutSoftware = new QAction("关于软件",this); helpMenu->addAction(aboutSoftware); connect(aboutSoftware,SIGNAL(triggered()),this,SLOT(on_aboutSoftware())); howToUse = new QAction("如何使用",this); helpMenu->addAction(howToUse); connect(howToUse,SIGNAL(triggered()),this,SLOT(on_howToUse())); connect(text1,SIGNAL(textChanged()),this,SLOT(on_changed())); } MainWindow::~MainWindow() { } void MainWindow::on_new() { if(isChanged){ QMessageBox::information(this,"提示","文件尚未保存"); }else{ text1->setText("");//新建的话把,文本内容置空。 } } void MainWindow::on_open() { if(isChanged){ QMessageBox::information(this,"提示","文件尚未保存"); }else{ fileNameString = QFileDialog::getOpenFileName(this,"打开");//当前窗体,打开文件的对话框的标题是:“打开” if(fileNameString==NULL){ return ; } FILE *pf = fopen(fileNameString.toStdString().data(),"r+");//可读可写的方式打开。 if(pf==NULL) return ; char buf[1024]; QString str; while(!feof(pf)){ fgets(buf,sizeof(buf),pf); str+=buf; } text1->setText(str); fclose(pf); } } void MainWindow::on_save() { fileNameString = QFileDialog::getSaveFileName(this,"保存"); if(fileNameString==NULL) return ; FILE *pf = fopen(fileNameString.toStdString().data(),"w+"); if(pf==NULL) return ; QString str = text1->toPlainText(); fputs(str.toStdString().data(),pf); fclose(pf); isChanged = true; } void MainWindow::on_quit() { if(isChanged){ QMessageBox::information(this,"提示","文件尚未保存,点击右上角×,取消保存"); }else{ this->close(); } } void MainWindow::on_copy() { text1->copy(); } void MainWindow::on_paste() { text1->paste(); } void MainWindow::on_cut() { text1->cut(); } void MainWindow::on_allSelect() { text1->selectAll(); } void MainWindow::on_howToUse() { QMessageBox::information(this,"如何使用","同记事本基本相同"); } void MainWindow::on_aboutSoftware() { QMessageBox::information(this,"关于软件","Realized By : Letben"); } void MainWindow::on_changed() { isChanged = true; }
开发结果:
时间: 2024-11-18 03:58:07