Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Displaying multiple images cost too much of RAM
Qt 6.12 LTS is out! See what's new in the release blog

Displaying multiple images cost too much of RAM

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SamurayHS Offline
    SamurayHS Offline
    SamurayH
    wrote on last edited by SamurayH
    #1

    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.

    "قال رسول الله صلى الله عليه وسلم : " أحب الناس إلى الله أنفعهم للناس

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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();
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      SamurayHS 1 Reply Last reply
      4
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @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 ?

        1 Reply Last reply
        3
        • VRoninV VRonin

          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();
          
          SamurayHS Offline
          SamurayHS Offline
          SamurayH
          wrote on last edited by SamurayH
          #4

          @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.

          "قال رسول الله صلى الله عليه وسلم : " أحب الناس إلى الله أنفعهم للناس

          1 Reply Last reply
          0
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by
            #5

            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.

            The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

            SamurayHS 1 Reply Last reply
            5
            • Kent-DorfmanK Kent-Dorfman

              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.

              SamurayHS Offline
              SamurayHS Offline
              SamurayH
              wrote on last edited by
              #6

              @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.

              "قال رسول الله صلى الله عليه وسلم : " أحب الناس إلى الله أنفعهم للناس

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt
              • Unsolved