18#ifndef NUMERICDELEGATE_H
19#define NUMERICDELEGATE_H
21#include <QItemDelegate>
22#include <QDoubleSpinBox>
47 NumericDelegate(QWidget *parent =
nullptr,
double maxValue = 1000000000.0,
double minValue = -1000000000.0,
48 int decimals = 3,
double stepSize = 0.1,
double value = 0)
49 : QItemDelegate(parent), maxValue(maxValue), minValue(minValue), decimals(decimals), stepSize(stepSize), value(value) {}
58 QWidget *
createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index)
const override {
59 QDoubleSpinBox *editor =
new QDoubleSpinBox(parent);
60 editor->setMaximum(maxValue);
61 editor->setMinimum(minValue);
62 editor->setDecimals(decimals);
63 editor->setSingleStep(stepSize);
64 editor->setValue(value);
74 void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index)
const override {
75 QDoubleSpinBox *spinBox = qobject_cast<QDoubleSpinBox *>(editor);
76 spinBox->interpretText();
77 double value = spinBox->value();
78 model->setData(index, value, Qt::EditRole);
The NumericDelegate class provides a double spin box editor for numeric data in a view.
Definition numericdelegate.h:28
NumericDelegate(QWidget *parent=nullptr, double maxValue=1000000000.0, double minValue=-1000000000.0, int decimals=3, double stepSize=0.1, double value=0)
Constructor for the NumericDelegate class.
Definition numericdelegate.h:47
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Creates an editor widget for the given parent, style option, and model index.
Definition numericdelegate.h:58
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
Sets the model data based on the data of the editor widget.
Definition numericdelegate.h:74