Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT Thumbnails Dyamically
Qt 6.11 is out! See what's new in the release blog

QT Thumbnails Dyamically

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 4.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amarism
    wrote on last edited by
    #1

    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.

    Taz742T 1 Reply Last reply
    0
    • A amarism

      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.

      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by
      #2

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

      Do what you want.

      A 1 Reply Last reply
      1
      • A Offline
        A Offline
        amarism
        wrote on last edited by amarism
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.
          1 Reply Last reply
          0
          • Taz742T Taz742

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

            A Offline
            A Offline
            amarism
            wrote on last edited by
            #5

            @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"));

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              A 1 Reply Last reply
              0
              • mrjjM mrjj

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

                A Offline
                A Offline
                amarism
                wrote on last edited by
                #7

                @mrjj User select image one by one

                mrjjM 1 Reply Last reply
                0
                • A amarism

                  @mrjj User select image one by one

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  Taz742T 1 Reply Last reply
                  2
                  • mrjjM mrjj

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

                    Taz742T Offline
                    Taz742T Offline
                    Taz742
                    wrote on last edited by Taz742
                    #9

                    @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());
                        }
                    }
                    
                    

                    Do what you want.

                    mrjjM 1 Reply Last reply
                    2
                    • Taz742T Taz742

                      @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());
                          }
                      }
                      
                      
                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Taz742
                      Cool. a threaded image loader :)

                      1 Reply Last reply
                      2

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved