How to show QListView..?



  • 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.



  • You just forgot to add galleryView the the Widget layout



  • @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.



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



  • @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



  • 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);
    


  • @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



  • 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);
    
    }
    


  • @VRonin

    Sorry to say this Ronin same output.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.