View Full Version : LineEdit / SpinBox focus in problem

13th September 2008, 12:45
I wanted to create custom spin box with following requirements:
- before modified by user, it displays a custom (non-number) text
- when focused (by mouse click), it selects all text, so simple input will overwrite the old one
First I solved by using specialValueText and on the first change I erase it, but Im still struggling with the second... I tried to reimplement focusInEvent, but I never managed to take correct focus and text selected :confused: any idea?

15th September 2008, 11:22
Could you show us what you've done so far?

15th September 2008, 14:06
Everything I remember I tried:

subclass QSpinBox and reimplement its focusInEvent to do QSpinBox::focusInEvent and then selectAll
the same with just QLineEdit
tried to trick event by creating new event QEvent::FocusEvent with reason tab key (which behave the way I would like also click work) and in focusInEvent call QLineEdit::event with this new event
also I tried to follow QLineEdit::focusInEvent, but it is quite complicated and using staff from QLineEditPrivate class, which I dont know how to access from my subclassed class (and it is really nasty :confused: )
I created signal connected to selectAll slot and in focusInEvent was like QLineEdit::event and then emit my signal
default behavior of QAbstractSpinBox::focusInEvent looks quite simple


if (event->reason() == Qt::TabFocusReason || event->reason() == Qt::BacktabFocusReason) {

I tried to rewrite this into my subclass' focusInEvent (without first line) and add MouseFocusReason into condition

nothing worked :(
If you're interested how whole class looks like, it is simple subclassed QSpinBox... i needed also reimplement sizeHint, becouse default one is bugged (i guess) and doesnt correctly calculate size for specialTextValue

17th September 2008, 14:06
oh, and if it can somehow affect the problem, this subclassed spinbox is used as part of subclassed QDialog