QtQuick using QQuickTextNode
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I tried to use QQuickTextNode in a custom QQuickitem
but ended up not seeing anything on the screen (I know it is a private API)...
Could anyone tell me what I am doing wrong?
Header:
#pragma once
#include <QQuickItem>
#include <QTextLayout>
class TextItem: public QQuickItem {
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
public:
TextItem(QQuickItem *parent = nullptr);
~TextItem();
QString text() const;
public slots:
void setText(QString text);
signals:
void textChanged(QString text);
protected:
QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data) override;
private:
QString _text;
QTextLayout *_layout;
};
Implementation:
#include "text_item.h"
#include <QTextLayout>
#include <private/qquicktextnode_p.h>
TextItem::TextItem(QQuickItem *parent)
: QQuickItem(parent)
, _layout(new QTextLayout)
{
setFlag(ItemHasContents, true);
}
TextItem::~TextItem()
{
delete _layout;
}
QString TextItem::text() const
{
return _text;
}
void TextItem::setText(QString text)
{
if (_text == text)
return;
_text = text;
emit textChanged(_text);
}
QSGNode *TextItem::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *data)
{
QQuickTextNode *node = nullptr;
if (!oldNode) {
node = new QQuickTextNode(this);
} else {
node = static_cast<QQuickTextNode *>(oldNode);
}
_layout->setText(_text);
QQuickText::TextStyle textStyle;
node->addTextLayout(QPointF(5, 5), _layout);
return node;
}
c++ qt qt5 qtquick2
add a comment |
I tried to use QQuickTextNode in a custom QQuickitem
but ended up not seeing anything on the screen (I know it is a private API)...
Could anyone tell me what I am doing wrong?
Header:
#pragma once
#include <QQuickItem>
#include <QTextLayout>
class TextItem: public QQuickItem {
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
public:
TextItem(QQuickItem *parent = nullptr);
~TextItem();
QString text() const;
public slots:
void setText(QString text);
signals:
void textChanged(QString text);
protected:
QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data) override;
private:
QString _text;
QTextLayout *_layout;
};
Implementation:
#include "text_item.h"
#include <QTextLayout>
#include <private/qquicktextnode_p.h>
TextItem::TextItem(QQuickItem *parent)
: QQuickItem(parent)
, _layout(new QTextLayout)
{
setFlag(ItemHasContents, true);
}
TextItem::~TextItem()
{
delete _layout;
}
QString TextItem::text() const
{
return _text;
}
void TextItem::setText(QString text)
{
if (_text == text)
return;
_text = text;
emit textChanged(_text);
}
QSGNode *TextItem::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *data)
{
QQuickTextNode *node = nullptr;
if (!oldNode) {
node = new QQuickTextNode(this);
} else {
node = static_cast<QQuickTextNode *>(oldNode);
}
_layout->setText(_text);
QQuickText::TextStyle textStyle;
node->addTextLayout(QPointF(5, 5), _layout);
return node;
}
c++ qt qt5 qtquick2
add a comment |
I tried to use QQuickTextNode in a custom QQuickitem
but ended up not seeing anything on the screen (I know it is a private API)...
Could anyone tell me what I am doing wrong?
Header:
#pragma once
#include <QQuickItem>
#include <QTextLayout>
class TextItem: public QQuickItem {
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
public:
TextItem(QQuickItem *parent = nullptr);
~TextItem();
QString text() const;
public slots:
void setText(QString text);
signals:
void textChanged(QString text);
protected:
QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data) override;
private:
QString _text;
QTextLayout *_layout;
};
Implementation:
#include "text_item.h"
#include <QTextLayout>
#include <private/qquicktextnode_p.h>
TextItem::TextItem(QQuickItem *parent)
: QQuickItem(parent)
, _layout(new QTextLayout)
{
setFlag(ItemHasContents, true);
}
TextItem::~TextItem()
{
delete _layout;
}
QString TextItem::text() const
{
return _text;
}
void TextItem::setText(QString text)
{
if (_text == text)
return;
_text = text;
emit textChanged(_text);
}
QSGNode *TextItem::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *data)
{
QQuickTextNode *node = nullptr;
if (!oldNode) {
node = new QQuickTextNode(this);
} else {
node = static_cast<QQuickTextNode *>(oldNode);
}
_layout->setText(_text);
QQuickText::TextStyle textStyle;
node->addTextLayout(QPointF(5, 5), _layout);
return node;
}
c++ qt qt5 qtquick2
I tried to use QQuickTextNode in a custom QQuickitem
but ended up not seeing anything on the screen (I know it is a private API)...
Could anyone tell me what I am doing wrong?
Header:
#pragma once
#include <QQuickItem>
#include <QTextLayout>
class TextItem: public QQuickItem {
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
public:
TextItem(QQuickItem *parent = nullptr);
~TextItem();
QString text() const;
public slots:
void setText(QString text);
signals:
void textChanged(QString text);
protected:
QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data) override;
private:
QString _text;
QTextLayout *_layout;
};
Implementation:
#include "text_item.h"
#include <QTextLayout>
#include <private/qquicktextnode_p.h>
TextItem::TextItem(QQuickItem *parent)
: QQuickItem(parent)
, _layout(new QTextLayout)
{
setFlag(ItemHasContents, true);
}
TextItem::~TextItem()
{
delete _layout;
}
QString TextItem::text() const
{
return _text;
}
void TextItem::setText(QString text)
{
if (_text == text)
return;
_text = text;
emit textChanged(_text);
}
QSGNode *TextItem::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *data)
{
QQuickTextNode *node = nullptr;
if (!oldNode) {
node = new QQuickTextNode(this);
} else {
node = static_cast<QQuickTextNode *>(oldNode);
}
_layout->setText(_text);
QQuickText::TextStyle textStyle;
node->addTextLayout(QPointF(5, 5), _layout);
return node;
}
c++ qt qt5 qtquick2
c++ qt qt5 qtquick2
edited Dec 9 '18 at 6:01
eyllanesc
87.8k103564
87.8k103564
asked Nov 26 '18 at 21:38
NilsNils
5,4731475101
5,4731475101
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You have to create the QTextLine of the QTextLayout, in the following example I have added the lines with an increasing width:
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
}
Output:
The complete example you find here.
Thank you so much for such an elaborate answer! I was unaware of how to use the QTextLayout API properly.
– Nils
Nov 27 '18 at 7:49
Just got into my office and tried it. Works perfectly, accepted!
– Nils
Nov 27 '18 at 8:56
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53489455%2fqtquick-using-qquicktextnode%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have to create the QTextLine of the QTextLayout, in the following example I have added the lines with an increasing width:
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
}
Output:
The complete example you find here.
Thank you so much for such an elaborate answer! I was unaware of how to use the QTextLayout API properly.
– Nils
Nov 27 '18 at 7:49
Just got into my office and tried it. Works perfectly, accepted!
– Nils
Nov 27 '18 at 8:56
add a comment |
You have to create the QTextLine of the QTextLayout, in the following example I have added the lines with an increasing width:
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
}
Output:
The complete example you find here.
Thank you so much for such an elaborate answer! I was unaware of how to use the QTextLayout API properly.
– Nils
Nov 27 '18 at 7:49
Just got into my office and tried it. Works perfectly, accepted!
– Nils
Nov 27 '18 at 8:56
add a comment |
You have to create the QTextLine of the QTextLayout, in the following example I have added the lines with an increasing width:
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
}
Output:
The complete example you find here.
You have to create the QTextLine of the QTextLayout, in the following example I have added the lines with an increasing width:
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
}
Output:
The complete example you find here.
answered Nov 26 '18 at 23:08
eyllanesceyllanesc
87.8k103564
87.8k103564
Thank you so much for such an elaborate answer! I was unaware of how to use the QTextLayout API properly.
– Nils
Nov 27 '18 at 7:49
Just got into my office and tried it. Works perfectly, accepted!
– Nils
Nov 27 '18 at 8:56
add a comment |
Thank you so much for such an elaborate answer! I was unaware of how to use the QTextLayout API properly.
– Nils
Nov 27 '18 at 7:49
Just got into my office and tried it. Works perfectly, accepted!
– Nils
Nov 27 '18 at 8:56
Thank you so much for such an elaborate answer! I was unaware of how to use the QTextLayout API properly.
– Nils
Nov 27 '18 at 7:49
Thank you so much for such an elaborate answer! I was unaware of how to use the QTextLayout API properly.
– Nils
Nov 27 '18 at 7:49
Just got into my office and tried it. Works perfectly, accepted!
– Nils
Nov 27 '18 at 8:56
Just got into my office and tried it. Works perfectly, accepted!
– Nils
Nov 27 '18 at 8:56
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53489455%2fqtquick-using-qquicktextnode%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown