Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top