aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quickcontrols2/texteditor
diff options
context:
space:
mode:
Diffstat (limited to 'examples/quickcontrols2/texteditor')
-rw-r--r--examples/quickcontrols2/texteditor/doc/src/qtquickcontrols2-texteditor.qdoc4
-rw-r--r--examples/quickcontrols2/texteditor/documenthandler.cpp15
-rw-r--r--examples/quickcontrols2/texteditor/documenthandler.h7
-rw-r--r--examples/quickcontrols2/texteditor/qml/texteditor.qml19
4 files changed, 41 insertions, 4 deletions
diff --git a/examples/quickcontrols2/texteditor/doc/src/qtquickcontrols2-texteditor.qdoc b/examples/quickcontrols2/texteditor/doc/src/qtquickcontrols2-texteditor.qdoc
index 3e9a9268..28cf3f3b 100644
--- a/examples/quickcontrols2/texteditor/doc/src/qtquickcontrols2-texteditor.qdoc
+++ b/examples/quickcontrols2/texteditor/doc/src/qtquickcontrols2-texteditor.qdoc
@@ -30,7 +30,7 @@
\title Qt Quick Controls - Text Editor
\keyword Qt Quick Controls 2 - Text Editor
\ingroup qtquickcontrols2-examples
- \brief A QML app using Qt Quick Controls 2 and a C++ class to
+ \brief A QML app using Qt Quick Controls and a C++ class to
provide a fully-functional rich-text editor application.
The \e {Text Editor Example} presents a sample HTML file using the TextArea
@@ -77,7 +77,7 @@
The touch user interface is a simplified version of the text editor. It is
suitable for touch devices with limited screen size. The example uses
- \l{Using File Selectors with Qt Quick Controls 2}{file selectors} to load
+ \l{Using File Selectors with Qt Quick Controls}{file selectors} to load
the appropriate user interface automatically.
Unlike the desktop version, which uses top-level dialogs, the touch version
diff --git a/examples/quickcontrols2/texteditor/documenthandler.cpp b/examples/quickcontrols2/texteditor/documenthandler.cpp
index 25a83592..5def1b61 100644
--- a/examples/quickcontrols2/texteditor/documenthandler.cpp
+++ b/examples/quickcontrols2/texteditor/documenthandler.cpp
@@ -80,7 +80,11 @@ void DocumentHandler::setDocument(QQuickTextDocument *document)
if (document == m_document)
return;
+ if (m_document)
+ m_document->textDocument()->disconnect(this);
m_document = document;
+ if (m_document)
+ connect(m_document->textDocument(), &QTextDocument::modificationChanged, this, &DocumentHandler::modifiedChanged);
emit documentChanged();
}
@@ -370,3 +374,14 @@ void DocumentHandler::mergeFormatOnWordOrSelection(const QTextCharFormat &format
cursor.select(QTextCursor::WordUnderCursor);
cursor.mergeCharFormat(format);
}
+
+bool DocumentHandler::modified() const
+{
+ return m_document && m_document->textDocument()->isModified();
+}
+
+void DocumentHandler::setModified(bool m)
+{
+ if (m_document)
+ m_document->textDocument()->setModified(m);
+}
diff --git a/examples/quickcontrols2/texteditor/documenthandler.h b/examples/quickcontrols2/texteditor/documenthandler.h
index a6125bc3..97e55658 100644
--- a/examples/quickcontrols2/texteditor/documenthandler.h
+++ b/examples/quickcontrols2/texteditor/documenthandler.h
@@ -84,6 +84,8 @@ class DocumentHandler : public QObject
Q_PROPERTY(QString fileType READ fileType NOTIFY fileUrlChanged)
Q_PROPERTY(QUrl fileUrl READ fileUrl NOTIFY fileUrlChanged)
+ Q_PROPERTY(bool modified READ modified WRITE setModified NOTIFY modifiedChanged)
+
public:
explicit DocumentHandler(QObject *parent = nullptr);
@@ -124,6 +126,9 @@ public:
QString fileType() const;
QUrl fileUrl() const;
+ bool modified() const;
+ void setModified(bool m);
+
public Q_SLOTS:
void load(const QUrl &fileUrl);
void saveAs(const QUrl &fileUrl);
@@ -150,6 +155,8 @@ Q_SIGNALS:
void loaded(const QString &text);
void error(const QString &message);
+ void modifiedChanged();
+
private:
void reset();
QTextCursor textCursor() const;
diff --git a/examples/quickcontrols2/texteditor/qml/texteditor.qml b/examples/quickcontrols2/texteditor/qml/texteditor.qml
index a1cb3435..6c95335b 100644
--- a/examples/quickcontrols2/texteditor/qml/texteditor.qml
+++ b/examples/quickcontrols2/texteditor/qml/texteditor.qml
@@ -80,7 +80,7 @@ ApplicationWindow {
}
Shortcut {
sequence: StandardKey.Quit
- onActivated: Qt.quit()
+ onActivated: close()
}
Shortcut {
sequence: StandardKey.Copy
@@ -121,7 +121,7 @@ ApplicationWindow {
}
MenuItem {
text: qsTr("&Quit")
- onTriggered: Qt.quit()
+ onTriggered: close()
}
}
@@ -205,6 +205,14 @@ ApplicationWindow {
id: errorDialog
}
+ MessageDialog {
+ id : quitDialog
+ title: qsTr("Quit?")
+ text: qsTr("The file has been modified. Quit anyway?")
+ buttons: (MessageDialog.Yes | MessageDialog.No)
+ onYesClicked: Qt.quit()
+ }
+
header: ToolBar {
leftPadding: 8
@@ -449,4 +457,11 @@ ApplicationWindow {
onTriggered: colorDialog.open()
}
}
+
+ onClosing: {
+ if (document.modified) {
+ quitDialog.open()
+ close.accepted = false
+ }
+ }
}