gpt4 book ai didi

c++ - QtQuick 使用 QQuickTextNode

转载 作者:行者123 更新时间:2023-11-28 01:26:35 29 4
gpt4 key购买 nike

我尝试在自定义 QQuickitem 中使用 QQuickTextNode,但最终在屏幕上看不到任何内容(我知道这是一个私有(private) 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;
}

最佳答案

您必须创建 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 .

关于c++ - QtQuick 使用 QQuickTextNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53489455/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com