user3358451
user3358451

Reputation: 57

Adding a background image to a 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?

Upvotes: 2

Views: 2719

Answers (1)

folibis
folibis

Reputation: 12864

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

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

Upvotes: 6

Related Questions