Pixelator Example

Image non disponible

By default, QTreeView, QTableView, and QListView use a standard item delegate to display and edit a set of common data types that are sufficient for many applications. However, an application may need to represent items of data in a particular way, or provide support for rendering more specialized data types, and this often requires the use of a custom delegate.

In this example, we show how to use custom delegates to modify the appearance of standard views. To do this, we implement the following components:

  • A model which represents each pixel in an image as an item of data, where each item contains a value for the brightness of the corresponding pixel.

  • A custom delegate that uses the information supplied by the model to represent each pixel as a black circle on a white background, where the radius of the circle corresponds to the darkness of the pixel.

This example may be useful for developers who want to implement their own table models or custom delegates. The process of creating custom delegates for editing item data is covered in the Spin Box Delegate example.

ImageModel Class Definition

The ImageModel class is defined as follows:

 
Sélectionnez
class ImageModel : public QAbstractTableModel
{
    Q_OBJECT

public:
    ImageModel(QObject *parent = 0);

    void setImage(const QImage &image);

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;

private:
    QImage modelImage;
};

Since we only require a simple, read-only table model, we only need to implement functions to indicate the dimensions of the image and supply data to other components.

ImageModel Class Implementation

The constructor is trivial:

 
Sélectionnez
ImageModel::ImageModel(QObject *parent)
    : QAbstractTableModel(parent)
{
}

The setImage() function sets the image that will be used by the model:

 
Sélectionnez
void ImageModel::setImage(const QImage &image)
{
    beginResetModel();
    modelImage = image;
    endResetModel();
}

The QAbstractItemModel::reset() call tells the view(s) that the model has changed.

The rowCount() and columnCount() functions return the height and width of the image respectively:

 
Sélectionnez
int ImageModel::rowCount(const QModelIndex & /* parent */) const
{
    return modelImage.height();
}

int ImageModel::columnCount(const QModelIndex & /* parent */) const
{
    return modelImage.width();
}

Since the image is a simple two-dimensional structure, the parent arguments to these functions are unused. They both simply return the relevant size from the underlying image object.

The data() function returns data for the item that corresponds to a given model index in a format that is suitable for a particular role: