Spin Box Delegate Example

The model/view framework provides a standard delegate that is used by default with the standard view classes. For most purposes, the selection of editor widgets available through this delegate is sufficient for editing text, boolean values, and other simple data types. However, for specific data types, it is sometimes necessary to use a custom delegate to either display the data in a specific way, or allow the user to edit it with a custom control.

Image non disponible

This concepts behind this example are covered in the Delegate Classes chapter of the Model/View Programming overview.

SpinBoxDelegate Class Definition

The definition of the delegate is as follows:

 
Sélectionnez
class SpinBoxDelegate : public QStyledItemDelegate
{
    Q_OBJECT

public:
    SpinBoxDelegate(QObject *parent = nullptr);

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const override;

    void setEditorData(QWidget *editor, const QModelIndex &index) const override;
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const override;

    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
                              const QModelIndex &index) const override;
};

The delegate class declares only those functions that are needed to create an editor widget, display it at the correct location in a view, and communicate with a model. Custom delegates can also provide their own painting code by reimplementing the paintEvent() function. Furthermore it is also possible to reuse (and avoid deleting) the editor widget by reimplementing the destroyEditor() function. A reused widget could be a mutable member created in the constructor and deleted in the destructor.

SpinBoxDelegate Class Implementation

Delegates are often stateless. The constructor only needs to call the base class's constructor with the parent QObject as its argument:

 
Sélectionnez
SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{
}

Since the delegate is a subclass of QStyledItemDelegate, the data it retrieves from the model is displayed in a default style, and we do not need to provide a custom paintEvent().

The createEditor() function returns an editor widget, in this case a spin box that restricts values from the model to integers from 0 to 100 inclusive.

 
Sélectionnez
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
                                       const QStyleOptionViewItem &/* option */,
                                       const QModelIndex &/* index */) const
{
    QSpinBox *editor = new QSpinBox(parent);
    editor->setFrame(false);
    editor->setMinimum(0);
    editor->setMaximum(100);

    return editor;
}

We install an event filter on the spin box to ensure that it behaves in a way that is consistent with other delegates. The implementation for the event filter is provided by the base class.

The setEditorData() function reads data from the model, converts it to an integer value, and writes it to the editor widget.

 
Sélectionnez
void SpinBoxDelegate::setEditorData(QWidget *editor,
                                    const QModelIndex &index) const
{
    int value = index.model()