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:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDir>
#include <QList>
#include <QDebug>
#include <QFileDialog>
#include <QStringListModel>
#include <QListView>
#include <QSortFilterProxyModel>
#include <QStandardItemModel>
namespace Ui {
class Widget;
}
{
Q_OBJECT
public:
explicit Widget
(QWidget *parent
= 0);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDir>
#include <QList>
#include <QDebug>
#include <QFileDialog>
#include <QStringListModel>
#include <QListView>
#include <QSortFilterProxyModel>
#include <QStandardItemModel>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
QStandardItemModel *model;
QListView *galleryView;
QSortFilterProxyModel *proxyModel;
};
#endif // WIDGET_H
To copy to clipboard, switch view to plain text mode
CPP File:
#include "widget.h"
#include "ui_widget.h"
ui(new Ui::Widget)
{
ui->setupUi(this);
QDir directory
("/u03/Commom_SelfDiag/BackUp/CSD_Tool/");
const QString folderPath
= directory.
filePath("images/");
if(!folderPath.isEmpty())
{
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);
ui->horizontalLayout->addWidget(galleryView);
}
Widget::~Widget()
{
delete ui;
}
#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;
}
To copy to clipboard, switch view to plain text mode
Main File
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
Widget w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
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.
Bookmarks