PDA

View Full Version : QSpinBox: how to disable line editing?



vonCZ
23rd April 2009, 12:50
I've looked thru all the members that take a "bool" parameter, yet I cannot find a way to disable line editing on QSpinBox. Seems like an obvious feature to have, I must be overlooking something.:confused:

Basically I want my spinBox to work with the arrows only; user shouldn't be able to type his own text in the 'lineEdit' part.

jpn
23rd April 2009, 12:52
Hint: QSpinBox inherits QAbstractSpinBox.

vonCZ
23rd April 2009, 13:27
I'd like to buy a vowel: A :p

Seriously: setReadOnly(true) does as expected--and inherets QAbstractSpinBox--but it disables the arrows, too. Is this what you were thinking?

Lykurg
23rd April 2009, 13:32
No, is to exp nsive. I give you the other ch r cters nd nother hint: QLineEdit inside the Q bstr ctSpinBox.

spirit
23rd April 2009, 13:57
No, is to exp nsive. I give you the other ch r cters nd nother hint: QLineEdit inside the Q bstr ctSpinBox.

wow, I don't understand nothing :D

vonCZ
23rd April 2009, 14:16
I still don't get it. Presumably something like:



QSpinBox *mbox = new QSpinBox;
mbox->someMember(QLineEdit::readOnly);




but I don't know. Or maybe something thru QAbstractSpinBox::setLineEdit. Obviously I'm unfamiliar with the way classes and the members they inherit relate to one another.

Of course I see LineEdit exists in AbstractSpinBox; but how to get at it? And to double-check: not suggesting I use QAbstractSpinBox instead of QSpinBox, right?

spirit
23rd April 2009, 14:33
you can do next this (it's a little bit tricky but works)


...
spinBox->installEventFilter(this);
...
bool Dialog::eventFilter(QObject *o, QEvent *e)
{
if (o == spinBox && e->type() == QEvent::KeyPress) {
e->ignore();
return true;
}
return QDialog::eventFilter(o, e);
}

vonCZ
23rd April 2009, 14:54
thanks spirit; yeah, this works. If there's no better solution, I'll just employ this.

I'm surprised QSpinBox doesn't do this off the shelf... but I've looked around and few--perhaps none--have asked about it.

jpn
23rd April 2009, 15:00
Ok, QAbstractSpinBox::lineEdit() is protected, but you can work it around like this:

spinBox->findChild<QLineEdit*>()->setReadOnly(true);
I just don't get why do you want to rip off an important functionality. It certainly decreases the usability of that input widget. At least make sure that shortcuts like up/down, pgup/pgdown etc. still work or your users will truly hate you. :P

vonCZ
23rd April 2009, 15:29
I just don't get why do you want to rip off an important functionality.

This is related to my other question about the 50 x 4,000 widget. This spinBox will have values 400 1200 2000, for example. The value selected in the spinBox will determine how much of that huge QWidget will be visible. In a sense: it allows the user to zoom into/out of the 50x4000 widget. After I get it all built and tested: perhaps I'll go back to not restricting the text.

Thanks for your help, btw.:)

jpn
23rd April 2009, 15:37
If you want to restrict spinbox text, the correct way is not to disable text input but to reimplement QAbstractSpinBox::validate() and/or QAbstractSpinBox::fixup() appropriately.