Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved How to show QListView..?

    General and Desktop
    2
    10
    2532
    Loading More Posts
    • 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.
    • M
      mounipanditi last edited by

      Hi i am learning to program with Model View and Delegate.
      I am trying an example to show images using QListView by reading some tutorials and Books, i have written some sample code, the code is executing fine but i am unable to see the QListView.
      I know i have done something wrong or missed to add some code snippet please make changes or post the code that makes my example to work so that i can be able to understand in detail.

      Here is the code

      Header File

      #ifndef WIDGET_H
      #define WIDGET_H
      
      #include <QWidget>
      #include <QDir>
      #include <QList>
      #include <QDebug>
      #include <QFileDialog>
      #include <QStringListModel>
      #include <QListView>
      #include <QSortFilterProxyModel>
      
      namespace Ui {
      class Widget;
      }
      
      class Widget : public QWidget
      {
          Q_OBJECT
          
      public:
          explicit Widget(QWidget *parent = 0);
          ~Widget();
          
      private:
          Ui::Widget *ui;
          QStringListModel *model;
          QListView *galleryView;
          QSortFilterProxyModel *proxyModel;
      };
      
      #endif // WIDGET_H
      

      CPP File

      #include "widget.h"
      #include "ui_widget.h"
      
      Widget::Widget(QWidget *parent) :
          QWidget(parent),
          ui(new Ui::Widget)
      {
          ui->setupUi(this);
      
          model   =     new QStringListModel(this);
          proxyModel	= new QSortFilterProxyModel(this);
          galleryView = new QListView;
      
          QStringList thumbnails;
          QDir directory("/u03/Commom_SelfDiag/BackUp/CSD_Tool/");
          const QString folderPath = directory.filePath("images/");
          if(!folderPath.isEmpty())
          {
              QDir dir(folderPath);
              QStringList filter;
              filter << QLatin1String("*.png");
              filter << QLatin1String("*.jpeg");
              filter << QLatin1String("*.jpg");
              dir.setNameFilters(filter);
              QFileInfoList filelistinfo = dir.entryInfoList();
              foreach (const QFileInfo &fileinfo, filelistinfo) {
      
              thumbnails << fileinfo.absolutePath();
              }
          }
          int count = thumbnails.count();
          qDebug()<<"The Total Images are"<<count;
      
          model->setStringList(thumbnails);
          proxyModel->setSourceModel(model);
          proxyModel->setFilterKeyColumn(0);
          galleryView->setViewMode(QListView::IconMode);
          galleryView->setModel(model);
          galleryView->setEditTriggers(QAbstractItemView::AnyKeyPressed |	QAbstractItemView::DoubleClicked);
      
      }
      
      Widget::~Widget()
      {
          delete ui;
      }
      
      

      Main

      #include <QtGui/QApplication>
      #include "widget.h"
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          Widget w;
          w.show();
          
          return a.exec();
      }
      
      

      Any help would be appreciated.

      1 Reply Last reply Reply Quote 0
      • VRonin
        VRonin last edited by VRonin

        You just forgot to add galleryView the the Widget layout

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        M 1 Reply Last reply Reply Quote 1
        • M
          mounipanditi @VRonin last edited by

          @VRonin

          Thanks for replying, i tried but i did not get how to add it can you please make the possible change in my code.

          1 Reply Last reply Reply Quote 0
          • VRonin
            VRonin last edited by

            Depends what layout you set in Qt Designer you need to add something similar to ui->horizontalLayout->addWidget(galleryView);

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            M 1 Reply Last reply Reply Quote 0
            • M
              mounipanditi @VRonin last edited by

              @VRonin

              thanks for the help Ronin, it worked but in order to see the images what is the change that i have to make please suggest me here you can see the output by using below link

              ImageLink-
              https://s3.postimg.org/s9qw2rsmb/Screenshot_from_2017_02_24_17_18_18.png

              1 Reply Last reply Reply Quote 0
              • VRonin
                VRonin last edited by

                replace the QStringListModel with QStandardItemModel and replace model->setStringList(thumbnails); with:

                model->insertColumn(0);
                const int numRows = thumbnails.size();
                model->insertRows(0,numRows );
                for(int i=0;i<numRows ;++i)
                model->setData(model->index(i,0),QPixmap(thumbnails.at(i)),Qt::DecorationRole);
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                M 1 Reply Last reply Reply Quote 0
                • M
                  mounipanditi @VRonin last edited by

                  @VRonin

                  Modified CPP

                  #include "widget.h"
                  #include "ui_widget.h"
                  
                  Widget::Widget(QWidget *parent) :
                      QWidget(parent),
                      ui(new Ui::Widget)
                  {
                      ui->setupUi(this);
                  
                      model   =     new QStandardItemModel(this);
                      proxyModel	= new QSortFilterProxyModel(this);
                      galleryView = new QListView;
                  
                      QStringList thumbnails;
                      QDir directory("/u03/Commom_SelfDiag/BackUp/CSD_Tool/");
                      const QString folderPath = directory.filePath("images/");
                      if(!folderPath.isEmpty())
                      {
                          QDir dir(folderPath);
                          QStringList filter;
                          filter << QLatin1String("*.png");
                          filter << QLatin1String("*.jpeg");
                          filter << QLatin1String("*.jpg");
                          dir.setNameFilters(filter);
                          QFileInfoList filelistinfo = dir.entryInfoList();
                          foreach (const QFileInfo &fileinfo, filelistinfo) {
                  
                          thumbnails << fileinfo.absolutePath();
                          }
                      }
                      int count = thumbnails.count();
                      qDebug()<<"The Total Images are"<<count;
                  
                      model->insertColumn(0);
                      const int numRows = thumbnails.size();
                      model->insertRows(0,numRows);
                      for(int i=0;i<numRows ;++i)
                      {
                      model->setData(model->index(i,0),QPixmap(thumbnails.at(i)),Qt::DecorationRole);
                      }
                      proxyModel->setSourceModel(model);
                      proxyModel->setFilterKeyColumn(0);
                      galleryView->setViewMode(QListView::IconMode);
                      galleryView->setModel(model);
                      galleryView->setEditTriggers(QAbstractItemView::AnyKeyPressed |	QAbstractItemView::DoubleClicked);
                  
                      ui->horizontalLayout->addWidget(galleryView);
                  
                  }
                  
                  Widget::~Widget()
                  {
                      delete ui;
                  }
                  

                  O/P image Link:
                  https://s22.postimg.org/9davetl5d/Screenshot_from_2017_02_24_17_47_54.png

                  1 Reply Last reply Reply Quote 0
                  • VRonin
                    VRonin last edited by VRonin

                    Try This:

                    Widget::Widget(QWidget *parent) :
                        QWidget(parent),
                        ui(new Ui::Widget)
                    {
                        ui->setupUi(this);
                    
                        model   =     new QStandardItemModel(this);
                        galleryView = new QListView(this);
                    
                        QDir directory("/u03/Commom_SelfDiag/BackUp/CSD_Tool/");
                        const QString folderPath = directory.filePath("images/");
                        if(!folderPath.isEmpty())
                        {
                            QDir dir(folderPath);
                            QStringList filter;
                            filter << QLatin1String("*.png");
                            filter << QLatin1String("*.jpeg");
                            filter << QLatin1String("*.jpg");
                            dir.setNameFilters(filter);
                            QFileInfoList filelistinfo = dir.entryInfoList();
                    model->insertColumn(0);
                      const int numRows = filelistinfo .size();
                    model->insertRows(0,numRows);
                             for(int i=0;i<numRows ;++i) {
                    #ifdef _DEBUG
                    QLabel * showLabel = new QLabel;
                    showLabel->setPixmap(QPixmap(filelistinfo.at(i).absolutePath()));
                    showLabel->show();
                    #endif
                    model->setData(model->index(i,0),QPixmap(filelistinfo.at(i).absolutePath()),Qt::DecorationRole);
                            }
                        }
                        
                        //galleryView->setViewMode(QListView::IconMode);
                        galleryView->setModel(model);
                        galleryView->setEditTriggers(QAbstractItemView::AnyKeyPressed |	QAbstractItemView::DoubleClicked);
                    
                        ui->horizontalLayout->addWidget(galleryView);
                    
                    }
                    

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    M 1 Reply Last reply Reply Quote 1
                    • M
                      mounipanditi @VRonin last edited by

                      @VRonin

                      Sorry to say this Ronin same output.

                      1 Reply Last reply Reply Quote 0
                      • VRonin
                        VRonin last edited by

                        Edited the code above, now it should spawn a window containing the image for each file, could you check if the windows work correctly?

                        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                        ~Napoleon Bonaparte

                        On a crusade to banish setIndexWidget() from the holy land of Qt

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post