Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QT Thumbnails Dyamically



  • I want to create Thumbnails(a group of an image) inside the ListWidget to select multiple images and have to display images one by one inside the Widget box using button click.



  • @amarism Hi.
    What are you tried ? What is problem?



  • This post is deleted!

  • Lifetime Qt Champion

    Hi
    You can use
    http://doc.qt.io/qt-5/qimage.html with
    http://doc.qt.io/qt-5/qimage.html#scaled
    to produce the thumbnails

    You can use something like

    //assume the directory exists and contains some files and you want all jpg and JPG files
    QDir directory("Pictures/MyPictures");
    QStringList images = directory.entryList(QStringList() << "*.jpg" << "*.JPG",QDir::Files);
    foreach(QString filename, images) {
    //do whatever you need to do
    }
    (thx to @the_ )
    ```.
    To grab all files from a folder to produce the thumbnails,
    
    - images one by one inside the Widget box 
    
    I dont know what widgetbox is, but you can use QScrollArea 
    http://doc.qt.io/qt-5/qscrollarea.html
    if you need any kinda of scrolling.


  • @Taz742 I tried to make a thumbnail inside ListWidget that select image from my computer dynamically. I will make a code for static but I need a dynamic image selection code.
    This is static image selection code.
    ui->setupUi(this);
    ui->listWidget->setViewMode(QListWidget::IconMode);
    ui->listWidget->setIconSize(QSize(130,130));
    ui->listWidget->setResizeMode(QListWidget::Adjust);
    ui->listWidget->addItem(new QListWidgetItem(QIcon("D:/qt_pic/LoginRed.jpg"),"logo"));
    ui->listWidget->addItem(new QListWidgetItem(QIcon("D:/qt_pic/1234.jpg"),"wallpaper"));
    ui->listWidget->addItem(new QListWidgetItem(QIcon("D:/qt_pic/4321.jpg"),"eye"));
    ui->listWidget->addItem(new QListWidgetItem(QIcon("D:/qt_pic/dog.jpg"),"Dog"));


  • Lifetime Qt Champion

    Hi
    So you need all files from a given folder or you need to let user
    select images one by one ?



  • @mrjj User select image one by one


  • Lifetime Qt Champion

    @amarism
    Ok. you can use normal QFileOpenDialog
    https://doc.qt.io/qt-5/qfiledialog.html#getOpenFileName
    to let user do that.



  • @mrjj + something like this:
    .h

    private:
        Ui::MainWindow*ui; 
        QMap<QString, QListWidgetItem*> m_mapFileNameListWidgetItem;
    

    .cpp

    void MainWindow::on_btnChooseImage_clicked()
    {
        QDir dr;
        QStringList list = QFileDialog::getOpenFileNames(this, QString("Choose Images"), dr.absolutePath(), tr("Format (*.png *.jpg *.jpeg))"));
        if (list.empty())
            return;
        startThread(list);
    }
    
    
    void MainWindow::startThread(QStringList list)
    {
        for (QString str : list) {
            if (m_mapFileNameListWidgetItem.find(str) != m_mapFileNameListWidgetItem.end())
                continue;
    
            QListWidgetItem *item = new QListWidgetItem(ui->listWidget);
            item->setSizeHint(QSize(150, 150);
            ui->listWidget->addItem(item);
            m_mapFileNameListWidgetItem[str] = item;
    
            QRunnableForImage *sub = new QRunnableForImage(str);
            connect(sub, SIGNAL(finished(QString, QImage, QByteArray)), this, SLOT(imageLoaded(QString, QImage, QByteArray)), Qt::QueuedConnection);
            QThreadPool::globalInstance()->start(sub);
        }
    }
    
    
    void MainWindow::imageLoaded(QString fileName, QImage img130x130, QByteArray bytes)
    {
        QListWidgetItem *item = m_mapFileNameListWidgetItem[fileName];
        if (!item) {
            return;
        }
        item->setIcon(QIcon(QPixmap::fromImage(img130x130)));
    }
    
    

    QRunnable .h

    #ifndef QRUNNABLEFORIMAGE_H
    #define QRUNNABLEFORIMAGE_H
    
    #include <QObject>
    #include "QRunnable"
    #include "QFile"
    #include "QImage"
    #include "QDebug"
    
    class QRunnableForImage : public QObject, public QRunnable
    {
        Q_OBJECT
    public:
        explicit QRunnableForImage(QString fileName, QObject *parent = 0);
    
        ~QRunnableForImage() {
            qDebug() << "qrunnableforimage deleted";
        }
    
    signals:
        void finished(QString filename, QImage img130x130, QByteArray bytes);
        void errorFound(QString what);
    
    public slots:
        void run();
    
    private:
        QString fileName;
    };
    
    #endif // QRUNNABLEFORIMAGE_H
    

    .cpp

    #include "qrunnableforimage.h"
    #include "QBuffer"
    
    QRunnableForImage::QRunnableForImage(QString fileName, QObject *parent) : QObject(parent)
    {
        this->fileName = fileName;
    }
    
    void QRunnableForImage::run()
    {
        QFile fl(this->fileName);
        if (fl.open(QIODevice::ReadOnly)) {
            QByteArray bytes = fl.readAll();
    
            QImage img130x130;
            img130x130= img130x130.fromData(bytes);
            img130x130= img130x130.scaled(130, 130, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    
            emit finished(this->fileName, img130x130, bytes);
        } else {
            emit errorFound(fl.errorString());
        }
    }
    
    

  • Lifetime Qt Champion

    @Taz742
    Cool. a threaded image loader :)


Log in to reply