QSpinBox
makes its contents selected (highlighted) upon using up/down buttons. Is there any way to disable this?
Is there any way to clear selection, other than use my own subclass of QSpinBox
to access the underlying QLineEdit
?
Asked
Active
Viewed 5,197 times
8

Violet Giraffe
- 32,368
- 48
- 194
- 335
2 Answers
11
There's no way to directly disable it, but you can do a bit of a hack:
void Window::onSpinBoxValueChanged() // slot
{
spinBox->findChild<QLineEdit*>()->deselect();
}
I recommend connecting to this using a queued connection, like this:
connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(onSpinBoxValueChanged()), Qt::QueuedConnection);
This will ensure that the slot is called after the line edit is highlighted.

Anthony
- 8,570
- 3
- 38
- 46
2
Same solution as @Anthony's, but shorter:
connect(spinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), spinBox,
[&, spinBox](){spinBox->findChild<QLineEdit*>()->deselect();}, Qt::QueuedConnection);

Adriel Jr
- 2,451
- 19
- 25
-
Thanks, but this is a duplicate of the already accepted answer (other than it uses the new connect syntax with lambda, which was not available at the time of writing). – Violet Giraffe Sep 21 '21 at 11:44
-
I agree, but still worth posting. – Adriel Jr Sep 21 '21 at 13:47