diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-12-19 11:16:23 +0100 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-12-20 13:18:02 +0100 |
commit | 99eaae4323ff1fda2d8cc0184d824b6d9c3f23ad (patch) | |
tree | 6c8073fcb263dff611e057df9b5a0653fce40f2b /src/widgets/doc/snippets/qitemdelegate | |
parent | 7996a3fc7f408a2f92b103f6d03d172b1f0d9295 (diff) |
Turn SpinBox Delegate example into snippets
The essence of the example was already fully quoted in the
model/view documentation. Move the code into a snippet
source, and update the screenshot.
Fixes: QTBUG-119976
Pick-to: 6.7 6.6
Change-Id: Id2f10bb26a650419969bbfa9b76cb74babd3319e
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Reviewed-by: Andreas Eliasson <andreas.eliasson@qt.io>
Diffstat (limited to 'src/widgets/doc/snippets/qitemdelegate')
-rw-r--r-- | src/widgets/doc/snippets/qitemdelegate/CMakeLists.txt | 12 | ||||
-rw-r--r-- | src/widgets/doc/snippets/qitemdelegate/spinbox-delegate.cpp | 79 |
2 files changed, 91 insertions, 0 deletions
diff --git a/src/widgets/doc/snippets/qitemdelegate/CMakeLists.txt b/src/widgets/doc/snippets/qitemdelegate/CMakeLists.txt new file mode 100644 index 0000000000..43f30c8aa5 --- /dev/null +++ b/src/widgets/doc/snippets/qitemdelegate/CMakeLists.txt @@ -0,0 +1,12 @@ +# Copyright (C) 2023 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +add_library(widgets_qitemdelegate_snippets OBJECT + spinbox-delegate.cpp +) + +target_link_libraries(widgets_qitemdelegate_snippets PRIVATE + Qt::Core + Qt::Gui + Qt::Widgets +) 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] + |