I have a QSpinBox in which I want to enable the arrows (for up and down values) but disable inserting data by the user. I've tried using this:
QtGui.QSpinBox.setReadOnly(True)
But it doesn't work. All is disabled and the arrows are 'stuck'.
I have a QSpinBox in which I want to enable the arrows (for up and down values) but disable inserting data by the user. I've tried using this:
QtGui.QSpinBox.setReadOnly(True)
But it doesn't work. All is disabled and the arrows are 'stuck'.
If you set the spin-box readonly, it will disable eveything. Instead, just set the line-edit readonly, and then buttons will still work:
spinbox.lineEdit().setReadOnly(True)
you can block spinboxes editor by QtGui.QSpinBox.lineEdit().setEnabled(False)
.
edit: and set font color and background-color:
spinbox.lineEdit().setStyleSheet('color: black; background-color: white;')
In case someone looking for an answer for this problem in C++ come here (like me), the other answers are not straightforward because QSpinBox::lineEdit() is a protected member (so it would require to also extend the class).
What worked for me was:
auto l1SpinBox = new QSpinBox(this);
auto lineEdit = l1SpinBox->findChild<QLineEdit*>();
lineEdit->setReadOnly(true);
lineEdit->setFocusPolicy(Qt::NoFocus);
connect(l1SpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), l1SpinBox,
[&, lineEdit](){lineEdit->deselect();}, Qt::QueuedConnection);
setReadOnly alone may do the trick for you. However in my case, I wanted to improve UI making it also not focusable, and then hiding the selection when the value changes.