Question

In my GUI I want to load several thumbnails from images. The images have a large size (maybe 3mb).

The images I want to load in a thread, so that the GUI doesn't freeze in this time. For this i tested to load the image as a QIcon in a QRunnable:

  ImageLoader::ImageLoader(QListWidgetItem *item, QString path)
  {
   this->path=path;
   this->item=item;
   }

  void ImageLoader::run()
  {
    QIcon icon(path);
    item->setIcon(icon);
  }

I've call this QRunnable with QThreadPool::globalInstance()->start(new ImageLoader(item,path));

But than there is a error: "QPixmap: It is not safe to use pixmaps outside the GUI thread".

What can i do, so that the gui isn't freeze?

Was it helpful?

Solution

Use QImage. It can be used from non-gui threads.

Loading the QImage can be done in the non-gui thread, but then anything that touches the GUI (in this case manipulating a QListWidgetItem) must be done back on the gui thread. This is ok because it's the loading and image decoding that takes most of the time.

Also, please see this article about the use of QThread: https://www.qt.io/blog/2010/06/17/youre-doing-it-wrong. Based on your code snippet, it looks like you may be falling into the trap described in the document.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top