Important: Please read the Qt Code of Conduct -

QGraphicsItem: How to repaint, bypassing the cache?

  • If a QGraphicsItem has the CacheMode set, e.g. to DeviceCoordinateCache, further calls to update() will no longer trigger the paint method, until something happens that tells the cache it needs to regenerate.

    What is this something? How can I tell the cache it needs to regenerate, and call my paint method?

    Edit: I know that a size change would invalidate the cache, at least for the DeviceCoordinateCache. However, I just want to change the contents, not the size of the item.

  • graphicsitem.cpp

    void QGraphicsItem::setCacheMode(CacheMode mode, const QSize &logicalCacheSize)
    CacheMode lastMode = CacheMode(d_ptr->cacheMode);
    d_ptr->cacheMode = mode;
    bool noVisualChange = (mode == NoCache && lastMode == NoCache)
    || (mode == NoCache && lastMode == DeviceCoordinateCache)
    || (mode == DeviceCoordinateCache && lastMode == NoCache)
    || (mode == DeviceCoordinateCache && lastMode == DeviceCoordinateCache);
    if (mode == NoCache) {
    } else {
    QGraphicsItemCache *cache = d_ptr->extraItemCache();

        // Reset old cache
        if (mode == ItemCoordinateCache) {
            if (lastMode == mode && cache->fixedSize == logicalCacheSize)
                noVisualChange = true;
            cache->fixedSize = logicalCacheSize;
    if (!noVisualChange)


    it seems that only a subsequent setCacheMode call for ItemCoordinateCache and then for DeviceCoordinateCache would repaint and recache again

    hope this works! :)


  • Changing the cache mode just for a repaint is not only ugly, it might even be costly (if an unnecessary cache item is generated and thrown away).

    I can't believe that caching can only be used on items whose contents never would be too big an oversight.

Log in to reply