Reputation: 6203
I'm using a QLineEdit field within a touch-controlled environment, that's why my question is a bit strange. There is a situation where a user may have typed some illegal data into the QLineEdit input field and should not be allowed to leave this input field until the illegal input has been corrected.
Now when I check the focus-lost-event for the QLineEdit, it is too late. This event is sent when e.g. an other button is clicked but this button is already processed at this point, and the probably wrong data in my input field are used.
So how can I solve this? Is there a possibility to lock the focus on an element so that all other widgets can't be used until this lock is released (disabling all the other widgets is NOT a solution)?
Or is there some kind of pre-focus-event where I can inhibit the event to the other widget and keep my input field active and focused exclusively?
Thanks!
Upvotes: 1
Views: 1271
Reputation: 9014
Solution is complex, but you should think about it...
I propose you to create a state machine that will handle enabled/disabled properties of all your GUI elements. When edit field gets a focus you should disable all other widgets, that may accept input. On text change event you should valiate data and if it became correct - then restore enabled property.
Some pseudo-code:
enum States { LockedState, NormalState };
class Validator
{
signals:
void valid();
void invalid();
slots:
void onTextChanged( QString text );
};
class Gui
{
public:
void initStateMachine();
Validator *_validator;
}
void Gui::initStateMachine()
{
_validator = new Validator();
QVector< QState * > _states;
_states.push_back( new QState() );
_states.push_back( new QState() );
// States
_states[LockedState]->assignProperty( ui->okbutton, "enabled", false );
_states[LockedState]->assignProperty( ui->cancelbutton, "enabled", false );
_states[LockedState]->assignProperty( ui->custombutton, "enabled", false );
_states[NormalState]->assignProperty( ui->okbutton, "enabled", true );
_states[NormalState]->assignProperty( ui->cancelbutton, "enabled", true );
_states[NormalState]->assignProperty( ui->custombutton, "enabled", true );
// Transitions
_states[LockedState]->addTransition( _validator, SIGNAL( valid() ), _states[NormalState] );
_states[NormalState]->addTransition( _validator, SIGNAL( invalid() ), _states[LockedState] );
// Go
for ( auto s : _states )
_stateMachine->addState( s );
_stateMachine->setInitialState( _states[NormalState] );
_stateMachine->setRunning( true );
connect( ui->textEdit, &textChanged, _validator, &onTextChanged );
}
Feel free to ask, if you need details.
Upvotes: 0