qtQuick源码中的loadImage函数

Posted

技术标签:

【中文标题】qtQuick源码中的loadImage函数【英文标题】:loadImage function in qtQuick source code 【发布时间】:2017-10-17 15:58:16 【问题描述】:

在 qtQuick 源中。函数loadImage中有一行代码:

QQuickPixmap* pix = new QQuickPixmap();

如果我在canvas上尽量调用这个函数。操作系统分配给这个应用程序的内存会被填满,导致图片失效。请问有没有问题这个源代码?

void QQuickCanvasItem::loadImage(const QUrl& url)


    Q_D(QQuickCanvasItem);
    QUrl fullPathUrl = d->baseUrl.resolved(url);
    if (!d->pixmaps.contains(fullPathUrl)) 
        QQuickPixmap* pix = new QQuickPixmap();
        QQmlRefPointer<QQuickCanvasPixmap> canvasPix;
        canvasPix.adopt(new QQuickCanvasPixmap(pix));
        d->pixmaps.insert(fullPathUrl, canvasPix);

        pix->load(qmlEngine(this)
                , fullPathUrl
                , QQuickPixmap::Cache | QQuickPixmap::Asynchronous);
        if (pix->isLoading())
            pix->connectFinished(this, SIGNAL(imageLoaded()));
    

【问题讨论】:

【参考方案1】:
void QQuickCanvasItem::loadImage(const QUrl& url)


    Q_D(QQuickCanvasItem);
    QUrl fullPathUrl = d->baseUrl.resolved(url);
    if (!d->pixmaps.contains(fullPathUrl)) 
        QQuickPixmap* pix = new QQuickPixmap();
        QQmlRefPointer<QQuickCanvasPixmap> canvasPix;
        canvasPix.adopt(new QQuickCanvasPixmap(pix));
        d->pixmaps.insert(fullPathUrl, canvasPix);

        pix->load(qmlEngine(this)
                , fullPathUrl
                , QQuickPixmap::Cache | QQuickPixmap::Asynchronous);
        if (pix->isLoading())
            pix->connectFinished(this, SIGNAL(imageLoaded()));
    

【讨论】:

以上是关于qtQuick源码中的loadImage函数的主要内容,如果未能解决你的问题,请参考以下文章