PDA

View Full Version : How to signal when a Qwidget receive focus



franco.amato
9th January 2010, 16:23
Hi,
I would signal to a QLed when a QWidget receive focus to accept keyboard input.
Something as:


connect( MyWidget, SIGNAL( focusReceived(bool) ),MyQLed, SLOT(setValue(bool)) );

but seems that QWidets class doesn't have such signal.
How can I do?

Best

faldzip
9th January 2010, 16:32
either subclass QWidget and reimplement focusInEvent and focusOutEvent to emit your custom signal or maybe simpler solution would be to install event filter on MyWidget and on focus events notify MyQLed about it.

franco.amato
9th January 2010, 16:38
either subclass QWidget and reimplement focusInEvent and focusOutEvent to emit your custom signal or maybe simpler solution would be to install event filter on MyWidget and on focus events notify MyQLed about it.

Dear faldzip, how can I install the event filter you mentioned?
Can you help me with a little example?

faldzip
9th January 2010, 17:32
There is a good example in Qt Docs: see QObject::eventFilter() and QObject::installEventFilter()

tokimay
5th February 2016, 10:33
classA::classA(){
...
...

ui->lineEdit->installEventFilter(this);

}

bool classA::eventFilter(QObject *object, QEvent *event)
{
if(event->type() == event->FocusIn && object == ui->lineEdit)
{
//do something
return true;
}
else
{
return false;
}
}

d_stranz
5th February 2016, 16:33
Are you getting some kind of thrill by replying to 5 and 6 year old dead posts? You aren't adding anything useful by duplicating what has already been said by previous posters. You have done nothing more than to essentially copy and paste the code that was linked to in faldzip's last post.

Read the forum, learn new things from it, but don't resurrect old posts if you have nothing new to add. Look at the date on the post before you reply to it. If the last reply was 6 years ago, let it rest in peace.