Skip to content

Commit c5006af

Browse files
committed
Don't do model updates while typing inside double/integer spinboxes, only when finished
1 parent b3a462d commit c5006af

5 files changed

Lines changed: 19 additions & 4 deletions

File tree

gui/source/pre/views/primitive/DoubleView.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ DoubleView::DoubleView(QAbstractItemModel* model, QPersistentModelIndex index, c
99

1010
// Set value from model and keep model up to date on changes
1111
setValue(model->data(index, Qt::DisplayRole).toDouble());
12-
QObject::connect(this, &DoubleSpinBox::valueChanged, this, [=](double value){ model->setData(index, value); });
12+
QObject::connect(this, &DoubleSpinBox::contentModified, this, [=, this]{ model->setData(index, this->value()); });
1313
}

gui/source/pre/views/primitive/IntegerView.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ IntegerView::IntegerView(QAbstractItemModel* model, QPersistentModelIndex index,
1111

1212
// Set value from model and keep model up to date on changes
1313
setValue(model->data(index, Qt::DisplayRole).toInt());
14-
QObject::connect(this, &QSpinBox::valueChanged, this, [=](int value){ model->setData(index, value); });
14+
QObject::connect(this, &IntegerSpinBox::contentModified, this, [=, this]{ model->setData(index, this->value()); });
1515
}

gui/source/pre/widgets/DoubleSpinBox.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ DoubleSpinBox::DoubleSpinBox(const Quantity& quantity, const DoubleRange& range,
1919
// Prevent catching focus when scrolling, https://stackoverflow.com/a/19382766
2020
setFocusPolicy(Qt::StrongFocus);
2121

22-
QObject::connect(lineEdit(), &QLineEdit::textEdited, this, &DoubleSpinBox::contentModified); // Signal modification by user when the text was edited
22+
QObject::connect(lineEdit(), &QLineEdit::editingFinished, this, &DoubleSpinBox::contentModified); // Signal modification by user when the text was edited
2323
QObject::connect(&quantity, &Quantity::unitChanged, this, &DoubleSpinBox::updateUnit);
2424
updateUnit();
2525
}
@@ -34,7 +34,7 @@ QString DoubleSpinBox::textFromValue(double baseValue) const {
3434
double unitValue = quantity.getUnit().fromBase(baseValue);
3535

3636
// Convert value to string with fixed-point representation
37-
// If the result has a decimal point, remove any trailing zeros and the point as well
37+
// If the result has a decimal point, remove any trailing zeros and possibly the point as well
3838
QString result = QString::number(unitValue, 'f', decimals());
3939
if(result.indexOf('.') != -1) {
4040
while(result.endsWith('0')) {

gui/source/pre/widgets/IntegerSpinBox.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ IntegerSpinBox::IntegerSpinBox(const IntegerRange& range, QWidget* parent)
1111

1212
// Prevent catching focus when scrolling, https://stackoverflow.com/a/19382766
1313
setFocusPolicy(Qt::StrongFocus);
14+
15+
// Signal modification by user when the text was edited
16+
// QSpinBox::valueChanged is not used because it is triggered during typing, leading to unnecessary updates of the model.
17+
QObject::connect(this, &QSpinBox::editingFinished, this, &IntegerSpinBox::contentModified);
1418
}
1519

1620
int IntegerSpinBox::valueFromText(const QString& text) const {
@@ -30,3 +34,8 @@ QValidator::State IntegerSpinBox::validate(QString& text, int& pos) const {
3034

3135
return QValidator::Acceptable;
3236
}
37+
38+
void IntegerSpinBox::stepBy(int steps) {
39+
QSpinBox::stepBy(steps);
40+
emit contentModified(); // Signal modification by user when the widget was stepped
41+
}

gui/source/pre/widgets/IntegerSpinBox.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44
struct IntegerRange;
55

66
class IntegerSpinBox: public QSpinBox {
7+
Q_OBJECT
8+
79
public:
810
IntegerSpinBox(const IntegerRange& range, QWidget* parent = nullptr);
911

12+
signals:
13+
void contentModified();
14+
1015
private:
1116
int valueFromText(const QString& text) const override;
1217
QValidator::State validate(QString &text, int &pos) const override;
18+
void stepBy(int steps) override;
1319
};

0 commit comments

Comments
 (0)