4

Let's suppose I have a QSpinBox with a value 123.45 in it. If I manually edit it and start erasing the five, valueChanged is fired for the value 123.4. Happens again if I go on erasing the four.

And it's also fired if I press enter after finishing editing.

I guess the problem is I should use void QAbstractSpinBox::editingFinished () instead of valueChanged, but it looks like valueChanged were the recommended approach as there are many more examples ans usage in my oppinion, so I want to be sure about this.

Any idea?

Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207
  • One more thing about difference between `editingFinished` and `valueChanged` with disabled `keyboardTracking`. `editingFinished` emitted only when focus has lost. User can set focus to `QSpinBox` and start scrolling. If you need update something during scrolling, use `valueChanged`. It is preferable approach in most cases. – Vladislav Jul 26 '17 at 10:13

3 Answers3

4

Finally I found the keyboardTracking property in Qt Documentation. Easy to set, and works like a charm!

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
Roman Rdgz
  • 12,836
  • 41
  • 131
  • 207
  • 5
    Please add some minimum info to your answer, so that people can understand without following the link. – HWende Jan 30 '13 at 16:28
  • 4
    That link is now broken. Try http://doc-snapshot.qt-project.org/4.8/qabstractspinbox.html#keyboardTracking-prop . And to summarise, from the page: If keyboard tracking is disabled, the spinbox doesn't emit the valueChanged() signal while typing. It emits the signal later, when the return key is pressed, when keyboard focus is lost, or when other spinbox functionality is used, e.g. pressing an arrow key. – Tim MB Feb 27 '13 at 11:17
  • 1
    Additional wrinkle that may confuse someone. Even when editingFinished signal is employed it may fire twice when debugging its handler in MS Visual Studio. Once on pressing return and if you have set a debugging break then handler will be invoked again because of focus change. – LRaiz Nov 27 '14 at 16:15
  • This is the correct answer but the link is broken as indicated above. Just in case other links break, the solution is to call setKeyboardTracking and explicitly set it to false. – astrofrog Apr 19 '17 at 13:32
  • @HWende I'm not the OP, but here's some additional info anyway because I didn't get it immediately. You can either set the property `keyboardTracking` to false in Qt Designer or in code by calling `setKeyboardTracking(false)` on the QSpinbox object. The property controls whether `valueChanged` should fire while typing or not. – Cerno Mar 14 '22 at 13:33
1

It is fine in my opinion to use either signal, several of the Qt form elements have both an editingFinished and a ????Changed signal.

  • QLineEdit
  • QAbstractSpinBox sub-classes:
    • QDateTimeEdit
    • QDoubleSpinBox
    • QSpinBox

The multi-line QTextEdit only has a textChanged as pressing return creates a new line not move focus.

Silas Parker
  • 8,017
  • 1
  • 28
  • 43
0

You might want to implement a key handler to only do something if e.g. the enter key was pressed. This is of course not as fast as valueChanged() but it might be more efficient...

could look like this

void MyWidget::keyPressEvent(QKeyEvent *event)
{
    int key = event->key();

    switch (key) {
        case Qt::Key_Return:
        case Qt::Key_Enter: {
            this->start();
            break;
        }
        case Qt::Key_Escape: {
            this->close();
            break;
        }
        default:
            QWidget::keyPressEvent(event);
    }
}

You would implement this not in your own MySpinBox class, but in the parent class. The enter key is passed from QSpinBox to it's parent because it is not handled. This is what is done at the end of the function if the key is not handled by MyWidget. It is then passed up to the base class.

HWende
  • 1,705
  • 4
  • 18
  • 30