Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Displaying multiple images cost too much of RAM



  • Hi,
    I'm trying to display multiple small images on the screen. The number of these images is user dependent (no limit).
    I use QLabel and QScrollArea, something like :

    QScrollArea *sA   = new QScrollArea;
    QWidget *sAW      = new QWidget;
    QVBoxLayout* vLyt = new QVBoxLayout;
    
    sA->setWidgetResizable(true);
    sAW->setLayout(vLyt);
    sA->setWidget(sAW);
    
    for(const QString& imgName : QDir("path/to/images/").entryList(QDir::Files))
    {
        QLabel* img = new QLabel;
        img->setFixedSize(140,80);
        img->setScaledContents(true); // the original size is much more bigger
        img->setPixmap(QPixmap("path/to/images/"+imgName));
        vLyt->addWidget(img);
    }
    sA->show();
    

    Once the images are displayed, my application memory usage increases by approximately 50 MB per 15 images. Isn't this too much for image displaying ?

    [EDIT]
    I think that there is a way to reduce the use memory. Because, for exemple, if I use HTML (<img src="img0.png"><img/>...) with QTextBrowser, I got the same result but without this increase in memory.
    That was incorrect. I was doing some bad testing :-/

    Unfortunately, I can't use the HTML approach, because I need some interaction with these images (hover, click...)
    [/EDIT]

    Thank you in advance.



  • You are just using the wrong tool. Use QListWidget in iconmode instead:

    QListWidget*list   = new QListWidget;
    list->setViewMode(QListView::IconMode);
    const QStringList pixList = QDir("path/to/images/").entryList(QDir::Files);
    for(const QString& imgName : pixList)
    {
    QListWidgetItem* tempItem = new QListWidgetItem(imgName);
    QPixmap tempImage("path/to/images/"+imgName);
    tempItem ->setData(Qt::DecorationRole,tempImage);
    list->addItem(tempItem);
    }
    list->show();
    

  • Lifetime Qt Champion

    @SamurayH said in Displaying multiple images cost too much of RAM:

    50 MB per 15 images. Isn't this too much for image displaying ?

    Hi
    How big is each file ?
    and what format ?



  • @VRonin said in Displaying multiple images cost too much of RAM:

    You are just using the wrong tool.

    I didn't know about QListWidget. But after testing it, I got the exact same result.

    @mrjj said in Displaying multiple images cost too much of RAM:

    How big is each file ?
    and what format ?

    About 60 ko to 90 ko, JPG image.

    I finally found my silly mistake, I wasn't scaling the QPixmap it self, by doing :

    img->setPixmap(QPixmap("path/to/images/"+imgName));
    

    instead of :

    img->setPixmap(QPixmap("path/to/images/"+imgName).scaled(140,80));
    

    Know for the same images, I have an increase of ~10Mo, instead of ~60Mo !
    But I think it still too much, no ?
    Because a 140x80 pixmap uses :140x80x(32/8)/1024 Ko (depth=32) = 43,75 Ko, for 15 images : ~655 Ko = ~0.6 Mo.



  • When I have to deal with many (1000s) of visual items I create a custom widget that intelligently caches the ones in or near the active viewing region. load on demand, and dispose of items items in a background thread: a nontrivial exercise unless someone knows an easier way in the framework.



  • @Kent-Dorfman said in Displaying multiple images cost too much of RAM:

    create a custom widget that intelligently caches the ones in or near the active viewing region

    This looks interesting.
    Unfortunately, uncompressing those QPixmap for each time they need to be drawn would be way too slow, check this.


Log in to reply