diff options
Diffstat (limited to 'src/widgets/doc/snippets/qitemdelegate/spinbox-delegate.cpp')
-rw-r--r-- | src/widgets/doc/snippets/qitemdelegate/spinbox-delegate.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/widgets/doc/snippets/qitemdelegate/spinbox-delegate.cpp b/src/widgets/doc/snippets/qitemdelegate/spinbox-delegate.cpp new file mode 100644 index 0000000000..efe8a16733 --- /dev/null +++ b/src/widgets/doc/snippets/qitemdelegate/spinbox-delegate.cpp @@ -0,0 +1,79 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include <QStyledItemDelegate> +#include <QSpinBox> + +//! [declaration] +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; +}; +//! [declaration] + +//! [constructor] +SpinBoxDelegate::SpinBoxDelegate(QObject *parent) + : QStyledItemDelegate(parent) +{ +} +//! [constructor] + +//! [createEditor] +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; +} +//! [createEditor] + +//! [setEditorData] +void SpinBoxDelegate::setEditorData(QWidget *editor, + const QModelIndex &index) const +{ + int value = index.model()->data(index, Qt::EditRole).toInt(); + + QSpinBox *spinBox = static_cast<QSpinBox*>(editor); + spinBox->setValue(value); +} +//! [setEditorData] + +//! [setModelData] +void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, + const QModelIndex &index) const +{ + QSpinBox *spinBox = static_cast<QSpinBox*>(editor); + spinBox->interpretText(); + int value = spinBox->value(); + + model->setData(index, value, Qt::EditRole); +} +//! [setModelData] + +//! [updateEditorGeometry] +void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, + const QStyleOptionViewItem &option, + const QModelIndex &/* index */) const +{ + editor->setGeometry(option.rect); +} +//! [updateEditorGeometry] + |