Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent QSpinBox from automatically highlighting contents

Tags:

qt

qt4

qspinbox

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?

like image 931
Violet Giraffe Avatar asked Oct 15 '12 08:10

Violet Giraffe


2 Answers

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.

like image 63
Anthony Avatar answered Oct 18 '22 13:10

Anthony


Same solution as @Anthony's, but shorter:

connect(spinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), spinBox,
            [&, spinBox](){spinBox->findChild<QLineEdit*>()->deselect();}, Qt::QueuedConnection);
like image 37
Adriel Jr Avatar answered Oct 18 '22 13:10

Adriel Jr