Qt Reference Documentation

Maemo 5 Text Edit Example


The Maemo 5 Text Edit example shows how to use QTextEdit or QPlainTextEdit within a QScrollArea.

Screenshot of the Text Edit example

The Maemo 5 style guide doesn't allow placing scrollable widgets within scrollable widgets. If a text edit is placed within a QScrollArea together with some other controls, the text edit shall not scroll, but adjust its size depending on the content. This allows the user to use finger scrolling to scroll the entire QScrollArea, independend on whether he scrolls on the text edit or some other control.

First, the top-level QScrollArea is created:

     QScrollArea area;

Then, the main widget for the QScrollArea is created:

     QWidget *w = new QWidget;
     QVBoxLayout *layout = new QVBoxLayout(w);

Then, two widgets are created. One QPushButton and a QTextEdit:

     QPushButton *button = new QPushButton("Clear");
     QTextEdit *edit = new QTextEdit;

Finally, the two widgets are added to the layout, and the main widget is added to the QScrollArea:




In the current state, the QTextEdit will do scrolling once the text won't fit into the widget, which is against the style guide. To prevent that, the convenience class TextEditAutoResizer (part of this example) can be used to automatically resize the QTextEdit once the contents become too large:

     new TextEditAutoResizer(edit);

The TextEditAutoResizer class also scrolls the QScrollArea when the user moves the cursor, ensuring that the cursor will be visible at all time.

To use the TextEditAutoResizer class in your own project, copy the header file texteditautoresizer.h to your own project. If you use qmake, ensure to add texteditautoresizer.h to your project's HEADERS variable.


Thank you for giving your feedback.

Make sure it is related to this specific page. For more general bugs and requests, please use the Qt Bug Tracker.

[0]; s.parentNode.insertBefore(ga, s); })();