Qt: fix crash on switching playlists quickly

This commit is contained in:
Brad Parker 2018-05-05 17:02:03 -04:00
parent a082ea7d76
commit 8e7e530747
2 changed files with 31 additions and 1 deletions

View File

@ -128,6 +128,7 @@ GridItem::GridItem() :
,image() ,image()
,pixmap() ,pixmap()
,imageWatcher() ,imageWatcher()
,mutex()
{ {
} }
@ -738,8 +739,12 @@ MainWindow::~MainWindow()
void MainWindow::onZoomValueChanged(int value) void MainWindow::onZoomValueChanged(int value)
{ {
if (m_gridItems.count() == 0)
return;
foreach(GridItem *item, m_gridItems) foreach(GridItem *item, m_gridItems)
{ {
QMutexLocker lock(&item->mutex);
int newSize = 0; int newSize = 0;
if (value < 50) if (value < 50)
@ -2816,11 +2821,16 @@ void MainWindow::removeGridItems()
if (item) if (item)
{ {
item->mutex.lock();
items.remove(); items.remove();
m_gridLayout->removeWidget(item->widget); m_gridLayout->removeWidget(item->widget);
delete item->widget; delete item->widget;
item->mutex.unlock();
delete item; delete item;
} }
} }
@ -2830,13 +2840,25 @@ void MainWindow::removeGridItems()
void MainWindow::onDeferredImageLoaded() void MainWindow::onDeferredImageLoaded()
{ {
const QFutureWatcher<GridItem*> *watcher = static_cast<QFutureWatcher<GridItem*>*>(sender()); const QFutureWatcher<GridItem*> *watcher = static_cast<QFutureWatcher<GridItem*>*>(sender());
GridItem *item = watcher->result(); GridItem *item = NULL;
if (!watcher)
return;
item = watcher->result();
if (!item)
return;
item->mutex.lock();
if (!item->image.isNull()) if (!item->image.isNull())
{ {
item->label->setPixmap(QPixmap::fromImage(item->image)); item->label->setPixmap(QPixmap::fromImage(item->image));
item->label->update(); item->label->update();
} }
item->mutex.unlock();
} }
void MainWindow::loadImageDeferred(GridItem *item, QString path) void MainWindow::loadImageDeferred(GridItem *item, QString path)
@ -2848,8 +2870,15 @@ void MainWindow::loadImageDeferred(GridItem *item, QString path)
GridItem* MainWindow::doDeferredImageLoad(GridItem *item, QString path) GridItem* MainWindow::doDeferredImageLoad(GridItem *item, QString path)
{ {
/* this runs in another thread */ /* this runs in another thread */
if (!item)
return NULL;
item->mutex.lock();
item->image = QImage(path); item->image = QImage(path);
item->mutex.unlock();
return item; return item;
} }

View File

@ -75,6 +75,7 @@ struct GridItem
QImage image; QImage image;
QPixmap pixmap; QPixmap pixmap;
QFutureWatcher<GridItem*> imageWatcher; QFutureWatcher<GridItem*> imageWatcher;
QMutex mutex;
}; };
class ThumbnailWidget : public QWidget class ThumbnailWidget : public QWidget