Adding a background image to a QQuickItem

I created QPixmap

and painted less QPixmap

on it with QPainter

. I want to use an image as a background QQuickItem

. Is there an easy way to do this?

+1


source to share


1 answer


If your custom element derived from QQuickItem

you can override QQuickItem::updatePaintNode()

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

      



Please note: your item is the owner QSGTexture *texture

, remember to delete it when you destroy the item.

+5


source







All Articles