QtQuick使用QQuickTextNode

时间:2018-11-26 21:38:09

标签: c++ qt qt5 qtquick2

我尝试在自定义QQuickitem中使用QQuickTextNode,但最终在屏幕上看不到任何内容(我知道这是一个私有API)...

有人可以告诉我我在做什么错吗?

标题:

#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;
};

实施:

#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;
}

1 个答案:

答案 0 :(得分:1)

您必须创建QTextLayout的QTextLine,在以下示例中,我添加了宽度增加的行:

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
}

输出:

enter image description here

找到完整的示例here