QTextEdit *textEdit = new QTextEdit; QTextDocument *doc = textEdit->document(); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(textEdit); doc->setTextWidth(textEdit->width()); setLayout(layout);
class CustomLayout : public QAbstractTextDocumentLayout { public: CustomLayout(QTextDocument *doc) : QAbstractTextDocumentLayout(doc) {} void draw(QPainter *painter, const PaintContext &context) override { // Implement custom drawing here } };In this example, we create a custom document layout by subclassing QAbstractTextDocumentLayout and implementing the draw() function. This allows us to perform custom drawing operations on the document layout, such as adding decorative elements or modifying the formatting of specific parts of the text. Package/Library: Qt Core Module.