summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2021-09-20 16:23:37 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2021-09-20 22:00:47 +0200
commita70ae1602d5c2f40ebe4a4ce05c2644e922e137d (patch)
tree7cfd983120e7158481652b6d02e5948bf9851931
parent3b439767a66f5e27b5fc71b6ea97f4cd5b6f0102 (diff)
Polish the markdown editor example
- Implement the exit handling using QWidget::closeEvent() - Mark PreviewPage::acceptNavigationRequest() as override - Use mime types with QFileDialog - Add status bar messages Pick-to: 6.2 Change-Id: I0fcbb1350f736a5ee04f266aac81942b7a269644 Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io> Reviewed-by: Michal Klocek <michal.klocek@qt.io>
-rw-r--r--examples/webenginewidgets/markdowneditor/mainwindow.cpp33
-rw-r--r--examples/webenginewidgets/markdowneditor/mainwindow.h4
-rw-r--r--examples/webenginewidgets/markdowneditor/previewpage.h4
3 files changed, 24 insertions, 17 deletions
diff --git a/examples/webenginewidgets/markdowneditor/mainwindow.cpp b/examples/webenginewidgets/markdowneditor/mainwindow.cpp
index 4ce2c1618..af5a6955b 100644
--- a/examples/webenginewidgets/markdowneditor/mainwindow.cpp
+++ b/examples/webenginewidgets/markdowneditor/mainwindow.cpp
@@ -56,6 +56,7 @@
#include <QFileDialog>
#include <QFontDatabase>
#include <QMessageBox>
+#include <QStatusBar>
#include <QTextStream>
#include <QWebChannel>
@@ -83,7 +84,7 @@ MainWindow::MainWindow(QWidget *parent) :
connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::onFileOpen);
connect(ui->actionSave, &QAction::triggered, this, &MainWindow::onFileSave);
connect(ui->actionSaveAs, &QAction::triggered, this, &MainWindow::onFileSaveAs);
- connect(ui->actionExit, &QAction::triggered, this, &MainWindow::onExit);
+ connect(ui->actionExit, &QAction::triggered, this, &QWidget::close);
connect(ui->editor->document(), &QTextDocument::modificationChanged,
ui->actionSave, &QAction::setEnabled);
@@ -109,6 +110,7 @@ void MainWindow::openFile(const QString &path)
}
m_filePath = path;
ui->editor->setPlainText(f.readAll());
+ statusBar()->showMessage(tr("Opened %1").arg(QDir::toNativeSeparators(path)));
}
bool MainWindow::isModified() const
@@ -139,12 +141,11 @@ void MainWindow::onFileOpen()
return;
}
- QString path = QFileDialog::getOpenFileName(this,
- tr("Open MarkDown File"), "", tr("MarkDown File (*.md)"));
- if (path.isEmpty())
- return;
-
- openFile(path);
+ QFileDialog dialog(this, tr("Open MarkDown File"));
+ dialog.setMimeTypeFilters({"text/markdown"});
+ dialog.setAcceptMode(QFileDialog::AcceptOpen);
+ if (dialog.exec() == QDialog::Accepted)
+ openFile(dialog.selectedFiles().constFirst());
}
void MainWindow::onFileSave()
@@ -165,25 +166,29 @@ void MainWindow::onFileSave()
str << ui->editor->toPlainText();
ui->editor->document()->setModified(false);
+
+ statusBar()->showMessage(tr("Wrote %1").arg(QDir::toNativeSeparators(m_filePath)));
}
void MainWindow::onFileSaveAs()
{
- QString path = QFileDialog::getSaveFileName(this,
- tr("Save MarkDown File"), "", tr("MarkDown File (*.md *.markdown)"));
- if (path.isEmpty())
+ QFileDialog dialog(this, tr("Save MarkDown File"));
+ dialog.setMimeTypeFilters({"text/markdown"});
+ dialog.setAcceptMode(QFileDialog::AcceptSave);
+ dialog.setDefaultSuffix("md");
+ if (dialog.exec() != QDialog::Accepted)
return;
- m_filePath = path;
+
+ m_filePath = dialog.selectedFiles().constFirst();
onFileSave();
}
-void MainWindow::onExit()
+void MainWindow::closeEvent(QCloseEvent *e)
{
if (isModified()) {
QMessageBox::StandardButton button = QMessageBox::question(this, windowTitle(),
tr("You have unsaved changes. Do you want to exit anyway?"));
if (button != QMessageBox::Yes)
- return;
+ e->ignore();
}
- close();
}
diff --git a/examples/webenginewidgets/markdowneditor/mainwindow.h b/examples/webenginewidgets/markdowneditor/mainwindow.h
index bb7a7ab01..fc18dde29 100644
--- a/examples/webenginewidgets/markdowneditor/mainwindow.h
+++ b/examples/webenginewidgets/markdowneditor/mainwindow.h
@@ -72,12 +72,14 @@ public:
void openFile(const QString &path);
+protected:
+ void closeEvent(QCloseEvent *e) override;
+
private slots:
void onFileNew();
void onFileOpen();
void onFileSave();
void onFileSaveAs();
- void onExit();
private:
bool isModified() const;
diff --git a/examples/webenginewidgets/markdowneditor/previewpage.h b/examples/webenginewidgets/markdowneditor/previewpage.h
index eb7d7ab00..2e7cfaeab 100644
--- a/examples/webenginewidgets/markdowneditor/previewpage.h
+++ b/examples/webenginewidgets/markdowneditor/previewpage.h
@@ -57,10 +57,10 @@ class PreviewPage : public QWebEnginePage
{
Q_OBJECT
public:
- explicit PreviewPage(QObject *parent = nullptr) : QWebEnginePage(parent) {}
+ using QWebEnginePage::QWebEnginePage;
protected:
- bool acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame);
+ bool acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame) override;
};
#endif // PREVIEWPAGE_H