将背景图片添加到 QQuickItem [英] Adding a background image to a QQuickItem

查看:84
本文介绍了将背景图片添加到 QQuickItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 QPixmap 并使用 QPainter 在其上绘制了较小的 QPixmap.我想使用图像作为 QQuickItem 的背景.是否有捷径可寻?

I have created a QPixmap and drawn smaller QPixmaps on it with QPainter. I want to use the image as the background of a QQuickItem. Is there an easy way to do this?

推荐答案

如果你的自定义项派生自 QQuickItem 你可以重新定义 QQuickItem::updatePaintNode() 在也许这个方式:

If your custom item derived from QQuickItem you can redefine QQuickItem::updatePaintNode() in maybe this way:

QSGNode *MyItem::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *)
{
    QSGSimpleTextureNode *node = static_cast<QSGSimpleTextureNode *>(oldNode);
    if (!node) {
        node = new QSGSimpleTextureNode();
        QSGTexture *texture = window()->createTextureFromImage(m_pixmap.toImage());
        node->setTexture(texture);
    }
    node->setRect(boundingRect());
    return node;
}

注意:你的物品是 QSGTexture *texture 的所有者,不要忘记在对象销毁时删除它.

Pay attention: your item is owner of QSGTexture *texture, don't forget to delete it while object destruction.

这篇关于将背景图片添加到 QQuickItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆