Skip to content

Commit

Permalink
Add save as method
Browse files Browse the repository at this point in the history
  • Loading branch information
rattle99 committed Feb 9, 2019
1 parent c08c344 commit c3bc494
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
19 changes: 16 additions & 3 deletions QtNotepad/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void MainWindow::on_actionOpen_triggered()
currentFile = fileName;
if(!file.open(QIODevice::ReadOnly | QFile::Text)){
QMessageBox::warning(this, "Warning", "Cannot open file : " + file.errorString());
return;
}
setWindowTitle(fileName);
QTextStream in(&file);
Expand All @@ -35,6 +36,18 @@ void MainWindow::on_actionOpen_triggered()
file.close();
}




void MainWindow::on_actionSave_As_triggered()
{
QString filename = QFileDialog::getSaveFileName(this, "Save as ");
QFile file(fileName);
if(!file.open(QFile::WriteOnly | QFile::Text)){
QMessageBox::warning(this, "Warning", "Cannot save file : " + file.errorString());
return;
}
currentFile = fileName;
setWindowTitle(fileName);
QTextStream out(&file);
QString text = ui->textEdit->toPlainText();
out << text;
file.close();
}
7 changes: 7 additions & 0 deletions QtNotepad/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class MainWindow : public QMainWindow
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void on_actionNew_triggered();

void on_actionOpen_triggered();

void on_actionSave_As_triggered();

private:
Ui::MainWindow *ui;
QString currentFile = "";
Expand Down

0 comments on commit c3bc494

Please sign in to comment.