PDA

View Full Version : Tab/Enter focus problem



b1
23rd October 2006, 00:28
G'Day All,

I have a simple function called from a QLineEdit on form A.

connect( lineEdit, SIGNAL( editingFinished( ) ), this, SLOT( checkMainDatabase() ) );

When I enter some data in the lineEdit and press "Enter", the function checks the main database and if not found opens the "add new data" form, B, and the focus is then on the new form, B. This part works as I planned.

My problem is this: when I press "tab" instead of "Enter" the same thing happens except that form B is placed behind the original form, A, and focus is set on the next QlineEdit, lineEdit_2 in form A.

By clicking on the "add new data" form the focus changes and all works normally. It is just annoying having to go throught the process of regaining the focus.

I am guessing the reason is that a "tab" from a QLineEdit causes the focus to changed to the next (in my case) QLineEdit on the original form whereas an "Enter" indicates editing finished and focus is changed by the progam code as I have written.

I have tried putting a setFocus command in the checkMainDatabase() function as a work around but to no avail. I have tried setting focus to the form B and widgets within the form.

Is there a way around this? Any thoughts or suggested reading would be appreciated.


Cheers, B1.

jpn
23rd October 2006, 09:08
Try adding these lines to where you show the "form B":


formB->show(); // you have already this
formB->raise(); // <--- add this
formB->activateWindow(); // <--- and this

b1
23rd October 2006, 22:28
Thanks for the reply JPN.

Unfortunately it didn't work. I have posted the code for the function below.

This is the call to open formB.

Thanks, b1.





void BackOrderForm::checkMainDatabase()
{
if (!lineEdit->text().isEmpty())
{
temppartnumber = lineEdit->text();
checkmaindqry.prepare( "SELECT description, brand FROM dbparts WHERE boffinspartno=(\'"+temppartnumber+"\')" );
checkmaindqry.exec();
checkmaindqry.next();
if (checkmaindqry.isValid())
{
lineEdit_2->setText(checkmaindqry.value(0).toString());
lineEdit_6->setText(checkmaindqry.value(1).toString());
}
else
{
TempNewPart = lineEdit->text();
formB = new UIDisplayPart::UIDisplayPart;
connect( formB->okButton, SIGNAL( clicked() ), this, SLOT( updateText() ) );
formB->show();
formB->raise();
formB->activateWindow();
formB->lineEdit_2->setFocus();
}
TempNewPart = "";
}
}

jacek
23rd October 2006, 23:06
formB = new UIDisplayPart::UIDisplayPart;
...
formB->show();
formB->raise();
formB->activateWindow();
QWidget::activateWindow() won't work for windows that aren't shown and new windows are shown after the control goes back to the event loop.

This might work:
void BackOrderForm::activateFormB()
{
formB->activateWindow();
}
...
QTimer::singleShot( 0, this, SLOT( activateFormB() ) );
// or if you don't like timers:
// QMetaObject::invokeMethod( this, "activateFormB", Qt::QueuedConnection );

Another solution you can try is:
formB->show();
formB->raise();
QApplication::processEvents(); // or QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
formB->activateWindow();

b1
23rd October 2006, 23:34
Thanks for the reply, jacek.

I tried all combinations of your suggestions and none work. I will go back to the drawing board and try recoding it all another way.

Thanks for the help so far, b1.