【问题标题】:loadImage function in qtQuick source codeqtQuick源码中的loadImage函数
【发布时间】: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()));
    }
}

【问题讨论】:

    标签: c++ qt canvas qml qtquick2


    【解决方案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()));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-19
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      相关资源
      最近更新 更多