load a thumbnail on QListWidget with reduced resolution

718 views Asked by At

I need to reduce the resolution of the images that I add and show in a QListWidgtet. Now I use the next code but it does not show all the images because they are being loaded with full resolution

void ImagesWizard::on_pbAddImages_clicked()
{
QFileDialog dialog(this);
dialog.setDirectory(mInitPath);
dialog.setFileMode(QFileDialog::ExistingFiles);
dialog.setNameFilter(trUtf8("Images (*.jpg *.png *.tif *.tiff *.bmp);; JPG (*.jpg);; PNG (*.png);; TIF (*.tif *.tiff);; BMP (*.bmp);;"));

QStringList filesToLoad;
if (dialog.exec())
    filesToLoad = dialog.selectedFiles();


if (filesToLoad.count()!=0) {
        QListWidget *localPathList= new QListWidget();
    localPathList->setViewMode(QListWidget::IconMode );
    localPathList->setIconSize(QSize(100,100));
    localPathList->setResizeMode(QListWidget::Adjust);
    localPathList->setSelectionMode(QAbstractItemView::MultiSelection);
    for (int var = 0; var < filesToLoad.count(); ++var) {
            if (!mImagesList->contains(filesToLoad[var])) {
                QFileInfo fileInfo(filesToLoad[var]);
                QString filename(fileInfo.fileName());
                QListWidgetItem *listItem = new QListWidgetItem(QIcon(filesToLoad[var]),filename);
                localPathList->addItem(listItem);
                mImagesList->append(filesToLoad[var]);
            }
    }
pbNext->setFocus();
}

}

Is there any way to resize the resolution in of the QIcon to optimice the time consumed to load the images and to show all the images added? Now if I load a lot of images the last QListWidgetItems only add the name.

Thanks for your help

1

There are 1 answers

1
UmNyobe On BEST ANSWER

Use QImage first to scale the image and construct the icon from the resulting pixmap.

QSize desiredSize;
Qimage orig(filesToLoad[var]);
Qimage scaled = orig.scaled(
            desiredSize,
            Qt::IgnoreAspectRatio,
            Qt::SmoothTransformation);

QListWidgetItem *listItem = new QListWidgetItem(QIcon(Qpixmap::fromImage(scaled)),filename);

It is very common to store the presized image too on the disk, to avoid the two step conversion process.