Adding a background image to a QQuickItem

旧城冷巷雨未停 提交于 2019-12-24 13:25:42

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/32162215/adding-a-background-image-to-a-qquickitem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!