Вы должны создать QTextLine для QTextLayout, в следующем примере я добавил строки с увеличивающейся шириной:
textitem.h
#ifndef TEXTITEM_H
#define TEXTITEM_H
#include <QQuickItem>
class QTextLayout;
class TextItem : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
public:
TextItem(QQuickItem *parent = nullptr);
~TextItem();
QString text() const;
void setText(const QString &text);
QFont font() const;
void setFont(const QFont &font);
signals:
void textChanged(const QString &);
void fontChanged();
protected:
QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *);
private:
QString m_text;
QFont m_font;
QTextLayout *m_layout;
};
#endif // TEXTITEM_H
textitem.cpp
#include "textitem.h"
#include <private/qquicktextnode_p.h>
#include <QFontMetrics>
TextItem::TextItem(QQuickItem *parent):
QQuickItem(parent),
m_layout(new QTextLayout)
{
setFlag(ItemHasContents, true);
}
TextItem::~TextItem(){ delete m_layout;}
QString TextItem::text() const{ return m_text; }
void TextItem::setText(const QString &text)
{
if(m_text == text) return;
m_text = text;
emit textChanged(m_text);
update();
}
QFont TextItem::font() const { return m_font;}
void TextItem::setFont(const QFont &font)
{
if(m_font == font) return;
m_font = font;
emit fontChanged();
update();
}
QSGNode *TextItem::updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *data)
{
Q_UNUSED(data)
QFontMetrics fontMetrics(font());
int height = 0;
int lineWidth = 40;
int leading = fontMetrics.leading();
m_layout->setText(m_text);
m_layout->beginLayout();
int counter = 1;
while (1) {
QTextLine line = m_layout->createLine();
if (!line.isValid())
break;
line.setLineWidth(counter*lineWidth);
height += leading;
line.setPosition(QPointF(0, height));
height += line.height();
counter++;
}
m_layout->endLayout();
QQuickTextNode *n = static_cast<QQuickTextNode *>(node);
if (!n)
n = new QQuickTextNode(this);
n->removeAllChildNodes();
n->addTextLayout(QPointF(5, 5), m_layout);
return n;
}
*. Qml
TextItem{
text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum volutpat libero elit, eu egestas lacus lacinia nec. Curabitur tempus lectus est, eget blandit arcu lacinia non. Curabitur vestibulum dictum diam ut hendrerit. Sed vitae ultricies libero. Morbi non libero risus. Aenean vel volutpat ipsum, scelerisque lacinia dui. Donec molestie vitae dolor vitae porttitor. Phasellus et facilisis elit. Sed metus lacus, commodo non interdum eu, viverra eu justo. Praesent a blandit nisi, ac porta magna. Aenean a lectus vel tortor ullamcorper ornare id non quam. Suspendisse potenti. Nunc hendrerit, eros nec vestibulum egestas, dolor orci malesuada est, sit amet tristique ipsum sem in mauris. Nullam lorem lacus, laoreet non venenatis sit amet, consequat dictum ligula. Quisque bibendum tellus egestas ex ullamcorper dapibus."
anchors.fill: parent
}
Вывод:
Полный пример вы найдете здесь .