Image thumbnail items
I asked in some other places and looked around for examples and tutorials for making a list of image thumbnails that doesn't block when it loads, but couldn't find anything.
The closest I got was using a QStandardItemModel and using a concurrent function that took a standard item and put the loaded image in its DecorationRole, but it segfaults when items are removed.
Seems like this should be a common problem, but I can't find a solution. Maybe you can suggest something?
Franzk last edited by
[quote author="MTK358" date="1286125749"]The closest I got was using a QStandardItemModel and using a concurrent function that took a standard item and put the loaded image in its DecorationRole, but it segfaults when items are removed.[/quote]
That sounds like something solvable. How do you manage your resources? Did you debug the thing? Where does it segfault? It usually is some pointer that still points to an address that is no longer in use (or pointing at NULL). You might want to share a bit of code if you need help. In any case, threaded/concurrent is probably the way I would have handled it.
I passed the QStandardItem to the concurrent function.
The QStandardItem stored the file's name in DisplayRole, and full path in FilePathRole (custom) and the concurrent function creates an image from the FilePathRole and puts in in DecorationRole. The reason for the custom role is because I don't want the user to see the full file path, just the name.
The problem is that when items are removed, all pointers to them become NULL, including those passed to not-yet-executed concurrent functions. I tried putting in an if statement to check if they are NULL, but that didn't help (I guess the OS switched to the main thread just after the if succeeded, and thenthe main thread made the pointer NULL).
VC15 last edited by
Probably you have some problems with thread-safe sharing of your pointers. Do you use QMutex or other means for controlling access to shared memory?
[quote author="VC15" date="1286352517"]Probably you have some problems with thread-safe sharing of your pointers. Do you use QMutex or other means for controlling access to shared memory?[/quote]
The problem is that loading images takes time, and that's why I used a thread in the first place. Stopping the threads from sleeping will defeat this. What's the purpose of using a thread if it will block the GUI anyway?
EDIT: I misunderstood what mutexes are. I don't think that would be a good idea, because I don't think i want to bother with mutexes throughout the main GUI thread.
But i got an idea. Do you think it would work good?
@void getThumbnail(QStandardItem item)
// stop thread from sleeping here
if (item != NULL)
path = item.data(ImagePathRole);
// let the thread sleep again here
QImage image(path); image = image.scaled(???, ???); // stop thread from sleeping here if (item != NULL) item.setData(QVariant<QImage>(image), Qt::DecorationRole);
If the item is deleted while the image is loading, it will just be thrown away.