在常见的记事本中,通常都有打开文件,写入文件、保存文件等功能,今天就用Qt来写一下记事本的打开、写入、保存等功能
一、创建菜单栏
QMenu *fileMenu = menuBar()->addMenu("File"); QToolBar *fileToolBar = addToolBar("File"); QAction *newAct = new QAction(QIcon(":/images/new.png"), "New", this); //添加快捷键 newAct->setShortcuts(QKeySequence::New); //状态栏提示 newAct->setStatusTip(tr("Create a new file")); connect(newAct, &QAction::triggered, this, &MainWindow::newFile); //添加菜单栏 fileMenu->addAction(newAct); //添加工具栏 fileToolBar->addAction(newAct); QAction *openAct = new QAction(QIcon(":/images/open.png"), "Open...", this); openAct->setShortcuts(QKeySequence::Open); openAct->setStatusTip("Open an existing file"); connect(openAct, &QAction::triggered, this, &MainWindow::open); fileMenu->addAction(openAct); fileToolBar->addAction(openAct); QAction *saveAct = new QAction(QIcon(":/images/save.png"), "Save", this); saveAct->setShortcuts(QKeySequence::Save); saveAct->setStatusTip("Save the document to disk"); connect(saveAct, &QAction::triggered, this, &MainWindow::save); fileMenu->addAction(saveAct); fileToolBar->addAction(saveAct); QAction *saveAsAct = fileMenu->addAction("Save As...", this, &MainWindow::saveAs, QKeySequence::SaveAs); saveAsAct->setStatusTip("Save the document under a new name"); //添加分割线 fileMenu->addSeparator(); QAction *exitAct = fileMenu->addAction("Exit", this, &QWidget::close); exitAct->setShortcuts(QKeySequence::Quit); exitAct->setStatusTip("Exit the application"); //创建状态栏 statusBar()->showMessage(tr("Ready"));
三、文件的写入并保存
bool MainWindow::save() { if (curFile.isEmpty()) { return saveAs(); } else { return saveFile(curFile); } }
bool MainWindow::saveAs() { QFileDialog dialog(this); dialog.setWindowModality(Qt::WindowModal); dialog.setAcceptMode(QFileDialog::AcceptSave); if (dialog.exec() != QDialog::Accepted) return false; return saveFile(dialog.selectedFiles().first()); }
bool MainWindow::saveFile(const QString &fileName) { QString errorMessage; QGuiApplication::setOverrideCursor(Qt::WaitCursor); QSaveFile file(fileName); if (file.open(QFile::WriteOnly | QFile::Text)) { QTextStream out(&file); out << textEdit->toPlainText(); if (!file.commit()) { errorMessage = tr("Cannot write file %1:\n%2.") .arg(QDir::toNativeSeparators(fileName), file.errorString()); } } else { errorMessage = tr("Cannot open file %1 for writing:\n%2.") .arg(QDir::toNativeSeparators(fileName), file.errorString()); } QGuiApplication::restoreOverrideCursor(); if (!errorMessage.isEmpty()) { QMessageBox::warning(this, "error", errorMessage); return false; } setCurrentFile(fileName); statusBar()->showMessage(tr("File saved"), 2000); return true; }
三、打开新文件
bool MainWindow::maybeSave() { //监测文本框中的内容是否修改,如何修改则保存 if (!textEdit->document()->isModified()) return true; const QMessageBox::StandardButton ret = QMessageBox::warning(this, "error", "The document has been modified.\n" "Do you want to save your changes?", QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); switch (ret) { case QMessageBox::Save: return save(); case QMessageBox::Cancel: return false; default: break; } return true; }
void MainWindow::open() { if (maybeSave()) { QString fileName = QFileDialog::getOpenFileName(this); if (!fileName.isEmpty()) loadFile(fileName); } }
void MainWindow::loadFile(const QString &fileName) { QFile file(fileName); if (!file.open(QFile::ReadOnly | QFile::Text)) { QMessageBox::warning(this, tr("Application"), tr("Cannot read file %1:\n%2.") .arg(QDir::toNativeSeparators(fileName), file.errorString())); return; } QTextStream in(&file); QGuiApplication::setOverrideCursor(Qt::WaitCursor); textEdit->setPlainText(in.readAll()); QGuiApplication::restoreOverrideCursor(); setCurrentFile(fileName); statusBar()->showMessage(tr("File loaded"), 2000); }
标签:return,Qt,QMessageBox,QAction,写入,fileName,file,菜单栏,MainWindow From: https://www.cnblogs.com/QingYiShouJiuRen/p/16849551.html