   Adding items to a QListView
  Hi, everyone, this is another newbie problem with Qt.I'm trying to make a scrollable list of images, and the best option so far seems to be using a QListView in IconMode. However, no matter how hard I search, I can't find a way to add items to that view. All the examples I've found so far are for Qt version 3.3 and use QListViewItems, which (apparently) no longer exist in Qt 4.5. I don't like using obsolete elements for my programs, but I really can't find how to do it with the modern version.Anybody knows how to add elements (specifically images) to a QListView in Qt 4.5? Should I drop the QListView altogether and go for some other option? Thanks in advance for any suggestions.   
  In Qt, there are 2 very different views to display data: ModelViews and ItemViews. The latter one work like their MFC counterpart (or QListItemView from Qt3), the first one (which you are using) only operates on a model.Models are there to communicate with views. What you need to do is create a Model that either directly contains the items, or holds a reference to the actual storage of item. Then you will fill that model with said data and attach the model to a view.I could point you to the tutorials, but I understand how hard this model view stuff can be, so here's my implementation to show an UndoStack in a QListView (which I wrote, until I noticed Qt already provided an Undo implementation, *sigh*):QActionStackModel.h:#pragma once#include "QActionStackItem.h"class QActionStackItemModel : public QAbstractItemModel{public:typedef QMap<ActionPtr,QActionStackItemPtr> ActionStackItems;private:ActionStackItems m_actions;public:/** * Add an action to the model. * * @throw   std::exception if the action has already been added to the model */void addAction( const ActionPtr &action );/** * Remove an action from the model. * * @throw   std::exception if the action has not been added to the model (or already removed) */void removeAction( const ActionPtr &action );/** * Change an actions committed state. * * @throw   std::exception if the action has not been added to the model */void changeAction( const ActionPtr &action, bool committed );private:/** * Get the item with the specified row. * * @throw   core::programming_error if there is no such item */QActionStackItemPtr item( int row ) const;virtual int rowCount( const QModelIndex &index = QModelIndex() ) const;virtual int columnCount( const QModelIndex &index = QModelIndex() ) const;virtual QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;virtual QModelIndex parent( const QModelIndex &index ) const;virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;};QActionStackModel.cpp#include "pch.h"#include "QActionStackItemModel.h"#include <boost/format.hpp>void QActionStackItemModel::addAction( const ActionPtr &action ){ActionStackItems::Iterator i = m_actions.find( action );if( i != m_actions.end() )throw std::excetion( "Cannot add the same action twice to the model" );QActionStackItemPtr item( new QActionStackItem( m_actions.size(), action->icon(), action->description() ) );beginInsertRows( QModelIndex(), item->row(), item->row() );m_actions.insert( action, item );endInsertRows();}void QActionStackItemModel::removeAction( const ActionPtr &action ){ActionStackItems::Iterator i = m_actions.find( action );if( i == m_actions.end() )throw std::exception( "Cannot remove the action from the mode: There is no corresponding item" );const QActionStackItemPtr &item = (*i);beginRemoveRows( QModelIndex(), item->row(), item->row() );m_actions.erase( i );endRemoveRows();}void QActionStackItemModel::changeAction( const ActionPtr &action, bool committed ){ActionStackItems::Iterator i = m_actions.find( action );if( i == m_actions.end() )throw std::exception( "Cannot change the action in the mode: There is no corresponding item" );const QActionStackItemPtr &item = (*i);item->setCommitted( committed );emit dataChanged( createIndex( item->row(), 0, item.get() ),  createIndex( item->row(), 0, item.get() ) );}QActionStackItemPtr QActionStackItemModel::item( int row ) const{foreach( const QActionStackItemPtr &item, m_actions ){if( item->row() == row )return item;}throw std::exception_error( boost::str( boost::format( "There is no item with the row '%1%'" ) % row ) );}int QActionStackItemModel::rowCount(const QModelIndex &/*index = QModelIndex */) const{return m_actions.size();}int QActionStackItemModel::columnCount(const QModelIndex &/*index = QModelIndex */) const{return 1;}QModelIndex QActionStackItemModel::index( int row, int column, const QModelIndex &parent ) const{if( row >= rowCount() )return QModelIndex();if( column >= columnCount() )return QModelIndex();// Find the item at that position, this is the data ptr...return createIndex( row, column, item( row ).get() );}QModelIndex QActionStackItemModel::parent( const QModelIndex &index ) const{return QModelIndex();}QVariant QActionStackItemModel::data( const QModelIndex &index, int role ) const{QActionStackItem *item = static_cast<QActionStackItem*>( index.internalPointer() );if( !item )throw std::exception( "internalPointer() of QModelIndex points to NULL, but it shouldn't" );switch( role ){case Qt::DecorationRole:{return item->icon();}break;case Qt::DisplayRole:{return item->text();}break;case Qt::BackgroundRole:{if( item->committed() ){return QVariant();}else{// When the item is not committed, this means// that it has been undone. The background shall// be gray for those onesreturn QBrush( QColor( 112, 128, 144 ) );}}break;default:{return QVariant();}break;}}I hope this is not too complicated for you. I'll try to explain it along the way. Basically there are 2 sets of functions in this class. The first ones (addAction, removeAction, changeAction) change the model's data. I use them to directly insert or remove an item to/from the model. The second set of functions (rowCount, columnCount, index, parent and data) implement the QAbstractItemModel class for a non-editable model(non-editable by the user, when interacting with the view).http://doc.trolltech.com/4.5 is also a great resource when working with Qt.