将背景图像添加到QQuickItem

use*_*451 1 qt qml qtquick2

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

fol*_*bis 5

如果您的自定义项目QQuickItem可以通过QQuickItem::updatePaintNode()以下方式重新定义:

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;
}
Run Code Online (Sandbox Code Playgroud)

注意:你的物品是物主QSGTexture *texture,不要忘记在物体破坏时删除它.