Results 1 to 2 of 2

Thread: How to display Images in QListView

  1. #1
    Join Date
    Nov 2014
    Posts
    54
    Thanks
    5
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default How to display Images in QListView

    Hi i am trying to display images in the ListView,
    I am new to Model View Delegate programming and i am trying to learn using that, i am getting a plain window with out images.

    Here is my code

    Header File:

    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QDir>
    6. #include <QList>
    7. #include <QDebug>
    8. #include <QFileDialog>
    9. #include <QStringListModel>
    10. #include <QListView>
    11. #include <QSortFilterProxyModel>
    12. #include <QStandardItemModel>
    13.  
    14. namespace Ui {
    15. class Widget;
    16. }
    17.  
    18. class Widget : public QWidget
    19. {
    20. Q_OBJECT
    21.  
    22. public:
    23. explicit Widget(QWidget *parent = 0);
    24. ~Widget();
    25.  
    26. private:
    27. Ui::Widget *ui;
    28. QListView *galleryView;
    29. QSortFilterProxyModel *proxyModel;
    30. };
    31.  
    32. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    CPP File:
    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3.  
    4. Widget::Widget(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::Widget)
    7. {
    8. ui->setupUi(this);
    9.  
    10. model = new QStandardItemModel(this);
    11. proxyModel = new QSortFilterProxyModel(this);
    12. galleryView = new QListView;
    13.  
    14. QStringList thumbnails;
    15. QDir directory("/u03/Commom_SelfDiag/BackUp/CSD_Tool/");
    16. const QString folderPath = directory.filePath("images/");
    17. if(!folderPath.isEmpty())
    18. {
    19. QDir dir(folderPath);
    20. QStringList filter;
    21. filter << QLatin1String("*.png");
    22. filter << QLatin1String("*.jpeg");
    23. filter << QLatin1String("*.jpg");
    24. dir.setNameFilters(filter);
    25. QFileInfoList filelistinfo = dir.entryInfoList();
    26. foreach (const QFileInfo &fileinfo, filelistinfo) {
    27.  
    28. thumbnails << fileinfo.absolutePath();
    29. }
    30. }
    31. int count = thumbnails.count();
    32. qDebug()<<"The Total Images are"<<count;
    33.  
    34. model->insertColumn(0);
    35. const int numRows = thumbnails.size();
    36. model->insertRows(0,numRows);
    37. for(int i=0;i<numRows ;++i)
    38. {
    39. model->setData(model->index(i,0),QPixmap(thumbnails.at(i)),Qt::DecorationRole);
    40. }
    41. proxyModel->setSourceModel(model);
    42. proxyModel->setFilterKeyColumn(0);
    43. galleryView->setViewMode(QListView::IconMode);
    44. galleryView->setModel(model);
    45. galleryView->setEditTriggers(QAbstractItemView::AnyKeyPressed | QAbstractItemView::DoubleClicked);
    46.  
    47. ui->horizontalLayout->addWidget(galleryView);
    48.  
    49. }
    50.  
    51. Widget::~Widget()
    52. {
    53. delete ui;
    54. }
    To copy to clipboard, switch view to plain text mode 

    Main File
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "widget.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Widget w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Me and my friend are trying this example and we are trying the QStandardItemModel but we are unable to solve.
    Please make necessary changes to make the code execute by displaying images.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,010
    Thanks
    233
    Thanked 636 Times in 626 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to display Images in QListView

    How do you know that you are actually loading the images into QPixmap? You don't do any error checking at all on this line of code:

    Qt Code:
    1. model->setData(model->index(i,0),QPixmap(thumbnails.at(i)),Qt::DecorationRole);
    To copy to clipboard, switch view to plain text mode 

    You just assume the QPixmaps are loaded and stored in the model. Break this line of code into two lines and check that you actually have a valid pixmap before storing it in the model:

    Qt Code:
    1. QPixmap pixmap( thumbnails.at( i ) );
    2. if ( !pixmap.isNull() )
    3. model->setData( model->index(i,0), pixmap, Qt::DecorationRole);
    4. else
    5. qDebug() << "Failed to load pixmap from file: " << thumbnails.at( i );
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QListView unresponsive when painting images
    By isplasher in forum Qt Programming
    Replies: 10
    Last Post: 5th May 2015, 18:29
  2. Replies: 2
    Last Post: 7th December 2013, 19:02
  3. How to show images from database as icons in QListView
    By LeshaS in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2011, 07:13
  4. QListView of images
    By scarleton in forum Newbie
    Replies: 6
    Last Post: 5th September 2010, 08:07
  5. Centering images in a QListView
    By scarleton in forum Newbie
    Replies: 3
    Last Post: 4th September 2010, 22:03

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.