PDA

View Full Version : QListView is white



reuabreliz
28th January 2010, 13: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

faldzip
28th January 2010, 16:55
You are returning some values (all QStrings) in data() and headerData() for every role, but there many different roles like Qt::ForegroundRole, Qt::SizeHintRole or Qt::BackgroundRole where your QStrings does not make sense.
Your values should be returned only for Qt::DisplayRole, for any other role return base class implementation. For example:


QVariant MZ_NoteListModel::data ( const QModelIndex & index, int role ) const
{
// check if it is some other role then Qt::DisplayRole
if (role != Qt::DisplayRole)
return QAbstractListModel::data(index, role);
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();
}


And your itemData() method is wrong. QMap<int, QVariant> is a map which assigns role with a value, not item row with role (which doesn't make sense as this method is called for particular index which has particular row).
I would suggest you not implementing itemData() and setItemData(), just implement data() and setData(). And you have mixed setData with setItemData, because your setData method has setItemData method's arguments, so in fact, you are not overriding any of this methods but creating your own third one. And remember that basic edit role (equivalent to Qt::DisplayRole) is Qt::EditRole, for other roles call base class implementation.
Surely you should get through Model Subclassing Reference chapter in Qt Assistant where everything is explained and check the examples.

reuabreliz
28th January 2010, 17:03
Oh ok. I didn't know that there are diffrent roles, i must handle diffrently. But now it works. Thanks very much.