Use QtConcurrent but main thread freeze in Qt

357 views Asked by At

There are thousands of images that want to make thumbnails and load them in list widget.

I use QtConcurrent to do it in a separate thread but the problem is that the main thread be freeze while they are loaded.

Here is my new thread:

void Dialog::on_treeView_doubleClicked(const QModelIndex &index)
{   
    connect(&secthread , &thread2::loadPicture , this ,&Dialog::on_loadPicture );
    QFuture<void> future2=QtConcurrent::run(&this->secthread , &thread2::start);
}

and function to show images :

void Dialog::on_loadPicture()
{
    ui->listWidget->setViewMode(QListWidget::IconMode);
    ui->listWidget->setIconSize(QSize(80,100 ));
    ui->listWidget->setResizeMode(QListWidget::Adjust);
    ui->progressBar->setMaximum(vec.size());
    ui->progressBar->setMinimum(l.min());
    QDirIterator it(path , QDirIterator::Subdirectories);
    connect(&l , SIGNAL(signalProgress(int)) , ui->progressBar , SLOT(setValue(int)));
    l.emit50();
    while(it.hasNext())
    {
        item=new QListWidgetItem(QIcon(it.next()) , QString("1 (" +  (QString::number(9)) + ").jpg"  ));
        QVariant pathData(it.filePath());
        item->setData(Qt::UserRole , pathData);
        ui->listWidget->addItem(item);
        this->repaint();
        ui->listWidget->repaint();
        n++;
        emit l.signalProgress(n);
    } 
}
0

There are 0 answers