[SOLVED] Asynchronous data loading for QFileSystemModel
-
Hi and welcome to devnet,
QFileSystemModel is already thread so you might be creating interference currently. You should maybe start creating your thumbnails only after directoryLoaded has been emitting so you are sure that all the information of the folder you are accessing is already available to the model.
Hope it helps
-
Hi, thanks for answer, but I already tried it and it didn't help.
-
Rather than using a mutex, why not return a "busy" image while you create the thumbnail ? Once it's created emitting data changed will trigger the update and you can return the thumbnail
-
@SGaist
Could you point me to some documentation about "busy" image please. I am new to Qt.
I thought returning empty QPixmap while thumbnail is loading is enough, but maybe this is causing the problem.
After thumbnail is loaded I am emitting dataChanged. -
By "busy image" I meant a non null pixmap containing e.g. a clock or whatever you see fit to make your user understand that some processing is occurring
-
Ok, I've tried to return some preloaded image while thumbnail is loading. After real thumbnail was loaded and dataChanged was emitted, element size in QListView keeps the same size as preloaded image. I've also tried to return QColor which is also acceptable as result by DecorationRole in data() function. In this case it looks same as when I was returning empty QPixmap. Elements was small.
So I think problem is somewhere else. It looks like QListView doesn't react properly when element size is changed by DecorationRole in model data() function. Or I am missing some function call.
One more observation, this happens only when widget with QListView is hidden and then it shows up. I have QListView on QTabWidget. Same implementation for view and model, but problem occurs on tab which is hidden and then showed up.
Window resize fixes the problem when resizeMode is equal QListView::Adjust -
Can you share the setup of your QListView ?
-
model = new MyModel(this); model->setRootPath(QDir::currentPath()); model->setReadOnly(false); model->setFilter(QDir::Files | QDir::Dirs | QDir::NoDot); model->setNameFilters(QStringList{ "*.my" }); model->setNameFilterDisables(false); ui->listView->setViewMode(QListView::IconMode); ui->listView->setModel(model); ui->listView->setRootIndex(_model->index(QDir::currentPath())); connect(ui->horizontalSliderIconSize, &QSlider::valueChanged, [=](int value) { ui->listView->setIconSize(QSize(value, value)); }); ui->horizontalSliderIconSize->setValue(128);
One more observation, there is difference depends on what you return from data() function.
If I return QIcon instead of QPixmap so code in data looks like:. . if (load) { QtConcurrent::run(const_cast<MyModel*>(this), &MyModel::loadThumbnail, info.filePath(), info.lastModified()); return QColor(Qt::gray); } else { return QIcon(pixmap); }
the problem is not so visible, but when you slide horizontalSliderIconSize a little bit you can see that decoration of elements changed
-
Problem was emitting dataChanged signal from different thread.
After using Qt::QueuedConnection everything works as intended. -
Sorry, I didn't saw my answer wasn't sent.
Indeed, dataChanged will trigger GUI update which is not possible from thread other than the GUI thread.