ayanda83
1st April 2015, 08:57
Guy are need your help with the code below. I have created a custom model to hold pixmaps in form of a list but the pictures are not displaying in the QListView.
#ifndef BACKGROUNDSLISTMODEL_H
#define BACKGROUNDSLISTMODEL_H
#include <QAbstractListModel>
#include <QLabel>
class BackgroundsListModel : public QAbstractListModel
{
public:
BackgroundsListModel();
~BackgroundsListModel();
Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
int rowCount(const QModelIndex &parent) const Q_DECL_OVERRIDE;
void loadBackgrounds();
private:
QList<QPixmap> pixmaps;
};
#endif // BACKGROUNDSLISTMODEL_H
#include "backgroundslistmodel.h"
BackgroundsListModel::BackgroundsListModel()
{
loadBackgrounds();
}
BackgroundsListModel::~BackgroundsListModel()
{
}
Qt::ItemFlags BackgroundsListModel::flags(const QModelIndex &index) const
{
if (index.isValid())
return (QAbstractListModel::flags(index)|Qt::ItemIsSelect able);
return Qt::ItemIsSelectable;
}
QVariant BackgroundsListModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
/*if (role == Qt::DecorationRole)
return QIcon(pixmaps.value(index.row()).scaled(m_PieceSiz e, m_PieceSize,
Qt::KeepAspectRatio, Qt::SmoothTransformation));*/
if (role == Qt::UserRole)
return pixmaps.value(index.row());
/*else if (role == Qt::UserRole + 1)
return locations.value(index.row());*/
return QVariant();
}
int BackgroundsListModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
else
return pixmaps.size();
}
void BackgroundsListModel::loadBackgrounds()
{
int row;
if (int(2.0 * qrand() / (RAND_MAX + 1.0)) == 1)
row = 0;
else
row = pixmaps.size();
for(int i = 1; i <= 5; i++){
QString imageDir = QString(":/Images/Backgrounds/background%1").arg(i);
QPixmap image(imageDir);
beginInsertRows(QModelIndex(), row, row);
pixmaps.insert(row, image);
endInsertRows();
}
}
and yes I did link the model to the view. Your help will be greatly appreciated.
thanking you in advance
Added after 1 5 minutes:
Can somebody help me out here guys. Your help will be greatly appreciated.
#ifndef BACKGROUNDSLISTMODEL_H
#define BACKGROUNDSLISTMODEL_H
#include <QAbstractListModel>
#include <QLabel>
class BackgroundsListModel : public QAbstractListModel
{
public:
BackgroundsListModel();
~BackgroundsListModel();
Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
int rowCount(const QModelIndex &parent) const Q_DECL_OVERRIDE;
void loadBackgrounds();
private:
QList<QPixmap> pixmaps;
};
#endif // BACKGROUNDSLISTMODEL_H
#include "backgroundslistmodel.h"
BackgroundsListModel::BackgroundsListModel()
{
loadBackgrounds();
}
BackgroundsListModel::~BackgroundsListModel()
{
}
Qt::ItemFlags BackgroundsListModel::flags(const QModelIndex &index) const
{
if (index.isValid())
return (QAbstractListModel::flags(index)|Qt::ItemIsSelect able);
return Qt::ItemIsSelectable;
}
QVariant BackgroundsListModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
/*if (role == Qt::DecorationRole)
return QIcon(pixmaps.value(index.row()).scaled(m_PieceSiz e, m_PieceSize,
Qt::KeepAspectRatio, Qt::SmoothTransformation));*/
if (role == Qt::UserRole)
return pixmaps.value(index.row());
/*else if (role == Qt::UserRole + 1)
return locations.value(index.row());*/
return QVariant();
}
int BackgroundsListModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
else
return pixmaps.size();
}
void BackgroundsListModel::loadBackgrounds()
{
int row;
if (int(2.0 * qrand() / (RAND_MAX + 1.0)) == 1)
row = 0;
else
row = pixmaps.size();
for(int i = 1; i <= 5; i++){
QString imageDir = QString(":/Images/Backgrounds/background%1").arg(i);
QPixmap image(imageDir);
beginInsertRows(QModelIndex(), row, row);
pixmaps.insert(row, image);
endInsertRows();
}
}
and yes I did link the model to the view. Your help will be greatly appreciated.
thanking you in advance
Added after 1 5 minutes:
Can somebody help me out here guys. Your help will be greatly appreciated.