PDA

View Full Version : Need help with the Model/View code.



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.

anda_skoa
1st April 2015, 10:10
Since you are not using any of the standard roles, you are obviously using a custom delegate, but you forgot to post its code.

Cheers,
_

ayanda83
1st April 2015, 10:50
@anda_skoa I am not familiar with using the model / view architecture but to respond to your post, except the custom model that is posted above, I have create no custom delegate instead I am trying to display the pixmaps on a QListView. Please explain to me what you think is broken with the code above. Below is the only other code I have.

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsScene>
#include <QLabel>
#include <QBoxLayout>
#include "backgroundslistmodel.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
BackgroundsListModel *model;
};

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("Elixir");

QBrush brush;
brush.setColor(Qt::cyan);

QGraphicsScene *scene; //scene for holdong items
scene = new QGraphicsScene(this); //instantiating the scene on the heap

scene->setSceneRect(-10, -10, 800, 1000);
scene->setBackgroundBrush(Qt::gray);

//ui->WorkArea_view->setBackgroundBrush(Qt::red);
ui->WorkArea_view->setScene(scene); //Linking the scence to the view
ui->WorkArea_view->setRenderHint(QPainter::Antialiasing);

model = new BackgroundsListModel();
ui->backgroundsView->setModel(model);
}

MainWindow::~MainWindow()
{
delete ui;
delete model;
}


#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}

yeye_olive
1st April 2015, 11:28
To paraphrase what anda_skoa said, BackgroundsListModel::data() returns QVariant() unless role == Qt::UserRole, which is why you do not see anything. How is the view supposed to know what user roles you want it to query the model for? Qt::DecorationRole is appropriate for pixmaps; why did you comment out the piece of code that used Qt::DecorationRole?

ayanda83
1st April 2015, 12:13
@yeye_olive, thank you for you reply. Uncommenting
Qt::DecorationRole is still not solving the problem. I have read the Qt documentation front to back and back to front. Something is missing here and I don't know what.

yeye_olive
1st April 2015, 15:43
Are the QPixmap loaded successfully? Try calling QPixmap::isNull() for each of them and check that you always get false.