Customizing List View Items

The Qt MVC classes allow us to customize the look and feel of our views by setting various roles on the data.
Here's an example using the QListView and standard data items. For a full list of roles, see: http://doc.qt.nokia.com/latest/qt.html

#include <QApplication>
#include <QListView>
#include <QStandardItemModel>
#include <QList>
#include <QColor>
#include <QFont>

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QStandardItemModel *model = new QStandardItemModel();

    QList<QString> colors;
    colors << "red" << "blue" << "cyan" << "magenta" << "black" << "yellow";
    QFont txtFont("Arial", 28);

    foreach (const QString &clrName, colors)
    {
        QStandardItem *colorItem = new QStandardItem(clrName);

// The Qt::BackgroundRole takes a brush and paints the background with it
//        colorItem->setData(QBrush(QColor(clrName)), Qt::BackgroundRole);

// The Qt::DecorationRole takes a color or a pixmap, and draws it in a small icon
// to the left of the icon
        colorItem->setData(QColor(clrName), Qt::DecorationRole);

// Qt::FontRole determines the font of the item
        colorItem->setData(txtFont, Qt::FontRole);

        colorItem->setData(QBrush(QColor(Qt::white).darker(160)),
                           Qt::ForegroundRole);

        model->appendRow(colorItem);
    }

    QListView w;
    w.setModel(model);
    w.show();

    app.exec();

}