reuabreliz
28th January 2010, 12:13
I have created my own QListModel through deriving from QAbstractListModel.
MZ_NoteListModel.h
#ifndef MZ_NOTELISTMODEL
#define MZ_NOTELISTMODEL
#include <QAbstractListModel>
#include <QObject>
class MZ_NoteListModel : public QAbstractListModel
{
public:
MZ_NoteListModel(QObject *parent = NULL);
QVariant headerData(int section, Qt::Orientation orientation, int role) const ;
int rowCount ( const QModelIndex & parent = QModelIndex() ) const ;
QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const ;
Qt::ItemFlags flags ( const QModelIndex & index ) const ;
bool setData ( const QModelIndex & index, const QMap<int, QVariant> & roles ) ;
QMap<int, QVariant> itemData ( const QModelIndex & index ) const ;
private:
};
#endif
#include "mz_noteListModel.h"
#include <QDebug>
MZ_NoteListModel::MZ_NoteListModel(QObject *parent) : QAbstractListModel(parent)
{
}
QVariant MZ_NoteListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal)
{
if (role == Qt::DisplayRole)
{
return QString("Notes");
}
}
return QVariant();
}
int MZ_NoteListModel::rowCount ( const QModelIndex & parent ) const
{
return 3;
}
QVariant MZ_NoteListModel::data ( const QModelIndex & index, int role ) const
{
if(index.isValid())
{
switch(index.row())
{
case 0:
return QString("0");
break;
case 1:
return QString("1");
break;
case 2:
return QString("2");
break;
default: return QVariant(); break;
}
}
return QVariant();
}
bool MZ_NoteListModel::setData ( const QModelIndex & index, const QMap<int, QVariant> & roles )
{
if( index.isValid() )
{
return true;
}
return false;
}
Qt::ItemFlags MZ_NoteListModel::flags ( const QModelIndex & index ) const
{
if(index.isValid())
{
return Qt::ItemIsEditable;
}
return 0;
}
QMap<int, QVariant> MZ_NoteListModel::itemData ( const QModelIndex & index ) const
{
QMap<int, QVariant> role;
if(index.isValid())
{
role[index.row()] = Qt::DisplayRole;
}
return role;
}
My Problem is, when I make a QListView and set the model to my MZ_NoteListModel, the View is only white. Nothing, no header, no rows, nothing!!
#include <iostream>
#include <QListView>
#include <QApplication>
#include "mz_noteListModel.h"
int main(int argc, char** argv)
{
QApplication q(argc, argv);
MZ_NoteListModel *noteModel = new MZ_NoteListModel();
QListView noteView;
noteView.setModel(noteModel);
noteView.show();
return q.exec();
}
Please, can anybody help me?
Thanks
MZ_NoteListModel.h
#ifndef MZ_NOTELISTMODEL
#define MZ_NOTELISTMODEL
#include <QAbstractListModel>
#include <QObject>
class MZ_NoteListModel : public QAbstractListModel
{
public:
MZ_NoteListModel(QObject *parent = NULL);
QVariant headerData(int section, Qt::Orientation orientation, int role) const ;
int rowCount ( const QModelIndex & parent = QModelIndex() ) const ;
QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const ;
Qt::ItemFlags flags ( const QModelIndex & index ) const ;
bool setData ( const QModelIndex & index, const QMap<int, QVariant> & roles ) ;
QMap<int, QVariant> itemData ( const QModelIndex & index ) const ;
private:
};
#endif
#include "mz_noteListModel.h"
#include <QDebug>
MZ_NoteListModel::MZ_NoteListModel(QObject *parent) : QAbstractListModel(parent)
{
}
QVariant MZ_NoteListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal)
{
if (role == Qt::DisplayRole)
{
return QString("Notes");
}
}
return QVariant();
}
int MZ_NoteListModel::rowCount ( const QModelIndex & parent ) const
{
return 3;
}
QVariant MZ_NoteListModel::data ( const QModelIndex & index, int role ) const
{
if(index.isValid())
{
switch(index.row())
{
case 0:
return QString("0");
break;
case 1:
return QString("1");
break;
case 2:
return QString("2");
break;
default: return QVariant(); break;
}
}
return QVariant();
}
bool MZ_NoteListModel::setData ( const QModelIndex & index, const QMap<int, QVariant> & roles )
{
if( index.isValid() )
{
return true;
}
return false;
}
Qt::ItemFlags MZ_NoteListModel::flags ( const QModelIndex & index ) const
{
if(index.isValid())
{
return Qt::ItemIsEditable;
}
return 0;
}
QMap<int, QVariant> MZ_NoteListModel::itemData ( const QModelIndex & index ) const
{
QMap<int, QVariant> role;
if(index.isValid())
{
role[index.row()] = Qt::DisplayRole;
}
return role;
}
My Problem is, when I make a QListView and set the model to my MZ_NoteListModel, the View is only white. Nothing, no header, no rows, nothing!!
#include <iostream>
#include <QListView>
#include <QApplication>
#include "mz_noteListModel.h"
int main(int argc, char** argv)
{
QApplication q(argc, argv);
MZ_NoteListModel *noteModel = new MZ_NoteListModel();
QListView noteView;
noteView.setModel(noteModel);
noteView.show();
return q.exec();
}
Please, can anybody help me?
Thanks