7. Text Widgets

While Qt buttons support binary input and numeric widgets support numerical input, text widgets allow plain text and rich text input. Qt provides three dedicated text input widgets:

  • QLineEdit - single-line plain-text editing,
  • QTextEdit - provides multi-line rich text with Markdown and HTML[^1] support,
  • QPlainTextEdit - multi-line plain-text with optional support for syntax coloring.

7.1 QLineEdit

QLineEdit provides a single-line plain-text editor. It supports the usual edit operations (copy, cut, paste, undo, redo) via standard keyboard shortcuts and a built-in context menu. You can restrict input length with setMaxLength(), validate input using a QValidator or input mask, and turn the widget into a password field by setting echoMode to EchoMode.Password.

The QLineEdit signals you are likely to use are:

Signal When Emitted
textChanged(text) Any change to the text (user or programmatic)
textEdited(text) Only when the user types, pastes, or deletes
returnPressed() Enter/Return key is pressed
editingFinished() Enter pressed or the line edit loses focus
selectionChanged() Selected text changes
inputRejected() A validator rejects the input
An icon of a clipboard-list

Your task is to build a wire transfer form for a finance application, adding a recipient name field that accepts only letters and warns the user about invalid input.

To u se a line edit in your application:

  1. Create a QLineEdit object for the recepient name, attach a regular expression validator that allows only letters, and add a QLabel to display feedback.

  2. Implement two slots, one to confirm the recipient name once editing finishes, and another to warn the user when an invalid character (a digit or a symbol that shouldn’t appear in a name) is rejected.

  3. Connect the editingFinished() and inputRejected() signal to the corresponding slots.

7.2 QTextEdit

QTextEdit is a widget for displaying and editing both plain text and rich text. It can handle large documents efficiently and can be used as an advanced WYSIWYG editor that support rich text formatting via HTML or Markdown. Internally, QtextEdit uses a QTextDocument object, which organizes content in a hierarchy of frames, blocks and fragments.

Commonly used QTextEdit signals include:

Signal Description
textChanged() Emitted when document’s content changes
cursorPositionChanged() Emitted when cursor position changes
copyAvailable(yes) Emitted when the availability of copy changes
redoAvailable(available) Emitted when redo becomes available/unavailable
undoAvailable(available) Emitted when undo becomes available/unavailable
selectionChanged() Emitted when current selection changes

Note that textChanged() is emitted for both user edits and programmatic changes such as calling setPlainText(), setHtml(), or setMarkdown().

An icon of a clipboard-list

Your task is to let users attach comments to a monthly budget report, formatted with Markdown, and showing a live preview of how it will render.

To use a text edit in your application:

  1. Create the source editor by instantiating a QTextEdit for editing the report notes, using a monospace font for better allignment.

  2. Create the preview widget as a second QTextEdit that will display the rendered notes, made read-only with a distinctive appearance.

  3. Implement the preview slot to and connect the signal. Get the source text edit’s contents as plain text and use setMarkdown() to render it as Markdown in the preview text edit. By default, setMarkdown() uses the GitHub-flavored Markdown dialect.

Note that the initial setPlainText() call happens only after connecting the textChanged() signal, ensuring the slot runs immediately and the preview is rendered with the initial text.

7.3 QPlainTextEdit

QPlainTextEdit provides a widget optimized for editing and displaying plain text. Unlike QTextEdit, it does not support HTML or Markdown. A QPlainTextEdit document is composed of characters and blocks (paragraphs) separated by newline characters, with each block containing a sequence of characters with optional formatting.

An icon of a clipboard-list

Your task is to add a risk rule editor to a finance application. Risk rules are stored as plain text and interpreted by the application’s rule engine. The editor uses a monospace font anf displays the current line, column, and total character count to help users edit and review rule definitions.

To use a plain text edit in your application:

  1. Create a QPlainTextEdit object, use QFontDatabase to set its font to a generic monospace font, and disable line wrapping. Also add two labels to the window: one for character count and one for cursor position.

  2. Implement the slots to display text stats. In update_char_count(), retrieve the character count directly from the editor’s underlying QTextDocument and display it in the label. A QTextDocument always contains at least one new line character, so an empty document will have the character count of 1. In update_position(), use the editor’s QTextCursor to get the current block number and the cursor position within the block, then calculate the current line (block number + 1) and column (position within the block + 1).

  3. Connect the signals to the slots. Update the character count on textChanged(), and update cursor position on cursorPositionChanged().

QTextDocument is the central part of the Scribe framework[^2], designed for representing and editing structured rich text documents. Representation is supported through document elements such as QTextBlock, QTextFrame, QTextTable and QTextList, and editing via the QTextCursor class. The framework also supports syntax highlighting.

The vertical dashed line in the diagram is a QTextCursor.

In Chapters 5, 6, and 7, we have examined widgets suitable for representing primitive data types. In Chapters 8, 9, and 10 we cover widgets designed to represent composite data types, including lists, tables, and trees.