Qt Quick Controls - Text Editor#

A rich-text editor app using Qt Quick Controls.

The Text Editor Example allows WYSIWYG editing of an HTML, Markdown or plain text file. The application comes with two user interfaces: one for larger screens, and a simplified UI for small touch-based devices. Both are “pure” QML. texteditor.cpp contains the main() function, which calls QFontDatabase::addApplicationFont() to add an icon font. (FontLoader would be an alternative way to achieve the same result.)

Desktop User Interface#

../_images/qtquickcontrols-texteditor-desktop.jpg

The desktop version is a complete text editor with capabilities for formatting text, and opening and saving HTML, Markdown and plain text files.

In the model-view-control (MVC) design pattern, the control layer includes the set of operations that can be performed. In Qt Quick Controls, the Action type is used to encapsulate a single operation or command. Accordingly, we begin with a set of Action objects:

The Action for opening a file must first prompt the user if the existing document has been changed, to avoid losing the user’s changes. Otherwise it simply opens the FileDialog which is declared further below.

The Action for saving the file is enabled only if there are changes to save:

The Action for copying selected text is enabled only if some text is selected:

Each Action to change text formatting (such as bold, italic and alignment) is checkable , and its boolean checked state is in sync with the relevant property in the selected text. Since declarative bidirectional synchronization is difficult, we use an onTriggered script to change the property when the Action is activated. The cursorSelection property is new in Qt 6.7 and makes this much easier than it was.

We have a MenuBar containing the hierarchy of Menus and MenuItems. Platform.MenuItem does not have an action property, so at minimum we need to set text and implement onTriggered.

Note

In Qt Quick Controls, each MenuItem could simply bind the relevant action . In a near-future version of Qt, Qt.labs.platform will be obsolete, and MenuBar will be suitable on every platform. Unless you need a native menu bar (only on platforms that provide one) in Qt 6.7 and older versions, you should avoid importing Qt.labs.platform. QtQuick.Controls is more portable.

...

The existing Action objects are reused in the ToolBar ; but here we override each Action’s text property to choose a textual icon from our icon font:

...

The main part of the text editor is a TextArea inside a Flickable:

...

A ScrollBar is attached to the vertical axis. Since word-wrapping is enabled via wrapMode, we don’t need a horizontal ScrollBar .

The TextArea.flickable attached property is used so that when the text cursor is moved out of the viewport (for example via arrow keys, or by typing a lot of text), TextArea scrolls the Flickable to keep the cursor visible.

There is a context menu; we use a TapHandler to detect a right-click and open it:

The context Menu contains MenuItems.

...

We consistently use the qsTr function to enable translation of UI text, so that the application will make sense regardless of the end user’s native language.

We use several kinds of dialogs:

It’s generally easier to declare separate instances for each purpose. We have two instances of FileDialog, for opening and saving files respectively. This became easier in Qt 6.7, with new features in TextDocument.

A FontDialog and a ColorDialog allow changing text formatting. (In Markdown format, there’s no syntax to represent specific font and color choices; but font characteristics such as bold, italic and monospace are saved. In HTML format, all formatting is saved.)

We have a MessageDialog to show error messages, and two more for prompting the user what to do when a file has been modified.

Touch User Interface#

../_images/qtquickcontrols-texteditor-touch.jpg

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 file selectors to load the appropriate user interface automatically.

Running the Example#

To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.

Example project @ code.qt.io