PDA

View Full Version : Howto handle QLineEdit::focusInEvent() from container?



scarleton
25th August 2010, 00:43
I want to do something to an QLineEdit when the user clicks on it. It looks like I need to capture the QLineEdit::focusInEvent(). I do NOT want to have to spin my own, is there some way to capture the event in the container dialog?

Lykurg
25th August 2010, 07:07
Install an event filter on your dialog: QObject::installEventFilter(). There you can catch the focus event of your line edit.

priyasuri
9th December 2010, 19:32
Hi,
I am a newbie to Qt programming. I was able to successfully install event filters on my objects when I coded the ui myself. I then decided to use Qt Designer to construct my UI but I am unable to figure out how event filters get installed on ui objects constructed using the designer. How do I installEventFilter using QtDesigner?

Thanks,
Priya

Lykurg
9th December 2010, 21:03
e.g.:
ui->NameOfYourWidget->installEventFilter(this);right in the constructor.

priyasuri
10th December 2010, 19:14
Hi Lykurg,
Thank you for the quick response. I did try installing the event filter using the method you suggested
ui->NameOfYourWidget->installEventFilter(this); but when I run the application, the two QLineEdit boxes to which I have installed the event filter show up blank. Is there a way to debug this ?

Thanks again for you time,
Priya

Hi Lykurg,
I managed to debug it myself. For some reason when I set the return value to false in the eventFilter function, the objects show up. Wonder if I am missing something..
Here is my eventFilter function...



bool QGridEventHandler::eventFilter(QObject *obj, QEvent *event)
{
bool bRetVal = false;
if (event->type() == QEvent::FocusIn) {
QLineEdit *grid = static_cast<QLineEdit *> (obj);
if (grid->text().isEmpty()){
if (bTurnChk) {
grid->setText("X");
bTurnChk = false;
}
else {
grid->setText("O");
bTurnChk = true;
}
}
}
return bRetVal;
}


Thanks,
Priya

I guess I should try myself before asking :) I just realized other events get triggered before the launch of the application, which need to be processed in the default manner. This happens only when the custom eventFilter function returns false if the QLineEdit's event is not processed or when the QObject's eventFilter function is called from within..
I have changed the code to the following and this works now...


bool QGridEventHandler::eventFilter(QObject *obj, QEvent *event)
{
bool bRetVal = true;
if (event->type() == QEvent::FocusIn) {
QLineEdit *grid = static_cast<QLineEdit *> (obj);
if (grid->text().isEmpty()){
if (bTurnChk) {
grid->setText("X");
bTurnChk = false;
}
else {
grid->setText("O");
bTurnChk = true;
}
}

}
else {
bRetVal = QObject::eventFilter(obj, event);
}
return bRetVal;
}

Thanks for your patience

Lykurg
10th December 2010, 19:24
Well, I guess then your event filter is the problem. What do you want to achieve and how does your event filter look like?