How to prevent QSpinBox from automatically highlighting contents
Asked Answered
U

2

8

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?

Underpay answered 15/10, 2012 at 8:50 Comment(0)
P
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.

Pricilla answered 15/10, 2012 at 11:32 Comment(0)
S
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);
Sturgeon answered 20/9, 2021 at 21:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.