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. How to show QListView..?
QtWS25 Last Chance

How to show QListView..?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 3.3k Views
  • 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 Offline
    M Offline
    mounipanditi
    wrote on 24 Feb 2017, 10:25 last edited by
    #1

    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
    0
    • V Offline
      V Offline
      VRonin
      wrote on 24 Feb 2017, 10:49 last edited by VRonin
      #2

      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 24 Feb 2017, 11:16
      1
      • V VRonin
        24 Feb 2017, 10:49

        You just forgot to add galleryView the the Widget layout

        M Offline
        M Offline
        mounipanditi
        wrote on 24 Feb 2017, 11:16 last edited by
        #3

        @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
        0
        • V Offline
          V Offline
          VRonin
          wrote on 24 Feb 2017, 11:21 last edited by
          #4

          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 24 Feb 2017, 11:52
          0
          • V VRonin
            24 Feb 2017, 11:21

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

            M Offline
            M Offline
            mounipanditi
            wrote on 24 Feb 2017, 11:52 last edited by
            #5

            @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
            0
            • V Offline
              V Offline
              VRonin
              wrote on 24 Feb 2017, 12:01 last edited by
              #6

              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 24 Feb 2017, 12:18
              0
              • V VRonin
                24 Feb 2017, 12:01

                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);
                
                M Offline
                M Offline
                mounipanditi
                wrote on 24 Feb 2017, 12:18 last edited by
                #7

                @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
                0
                • V Offline
                  V Offline
                  VRonin
                  wrote on 24 Feb 2017, 13:27 last edited by VRonin
                  #8

                  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 24 Feb 2017, 13:39
                  1
                  • V VRonin
                    24 Feb 2017, 13:27

                    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);
                    
                    }
                    
                    M Offline
                    M Offline
                    mounipanditi
                    wrote on 24 Feb 2017, 13:39 last edited by
                    #9

                    @VRonin

                    Sorry to say this Ronin same output.

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      VRonin
                      wrote on 24 Feb 2017, 13:43 last edited by
                      #10

                      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
                      0

                      9/10

                      24 Feb 2017, 13:39

                      • Login

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