NeTrainSim 0.1.1 beta
The Open-Source Network Trains Simulator
 
Loading...
Searching...
No Matches
numericdelegate.h
Go to the documentation of this file.
1
18#ifndef NUMERICDELEGATE_H
19#define NUMERICDELEGATE_H
20
21#include <QItemDelegate>
22#include <QDoubleSpinBox>
23
28class NumericDelegate : public QItemDelegate {
29 Q_OBJECT
30
31private:
32 double maxValue;
33 double minValue;
34 int decimals;
35 double stepSize;
36 double value;
37
38public:
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) {}
50
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);
65 return editor;
66 }
67
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);
79 }
80};
81
82#endif // NUMERICDELEGATE_H
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