franco.amato
13th July 2011, 23:07
Good morning to all, this is my scenario:
I wrote an application based on QMainWindow containing a centralwidget.
The centralwidget contains 2 classes inherited from QWidget lets say ww1 and ww2.
Now I would, pressing tab, move the focus between ww1 and ww2.
I tried to override the focusInEvent - focusOutEvent methods of every QWidget
class MyWidget : public QWidget
{
//code
protected:
virtual void focusInEvent( QFocusEvent* event );
virtual void focusOutEvent( QFocusEvent* event );
//more code
};Now I would, from the QMainWindow, to execute some code depending on the QWidget having the focus. Now I emit a signal in the focusIn/Out methods so:
/*********************************************/
/* focusInEvent
/*********************************************/
void MyWidget::focusInEvent( QFocusEvent *event )
{
Q_UNUSED(event);
emit sig_focusIn(); // widget had the focus
}
/*********************************************/
/* focusOutEvent
/*********************************************/
void MyWidget::focusOutEvent( QFocusEvent *event )
{
Q_UNUSED(event);
emit sig_focusOut(); // focus is gone
}The signals are connected to a slot that's part of mainwindow class. My code works but I'm not sure if is the best way to achieve what I would.
For example I read about a virtual bool eventFilter(QObject *obj, QEvent *evt); that can filter ( as it says ) the event sent to other widgets. For example intercepting the focus event. The problem is how can I know it the focus is for ww1 or ww2?
I hope to get a reply.
Best Regards,
Franco
I wrote an application based on QMainWindow containing a centralwidget.
The centralwidget contains 2 classes inherited from QWidget lets say ww1 and ww2.
Now I would, pressing tab, move the focus between ww1 and ww2.
I tried to override the focusInEvent - focusOutEvent methods of every QWidget
class MyWidget : public QWidget
{
//code
protected:
virtual void focusInEvent( QFocusEvent* event );
virtual void focusOutEvent( QFocusEvent* event );
//more code
};Now I would, from the QMainWindow, to execute some code depending on the QWidget having the focus. Now I emit a signal in the focusIn/Out methods so:
/*********************************************/
/* focusInEvent
/*********************************************/
void MyWidget::focusInEvent( QFocusEvent *event )
{
Q_UNUSED(event);
emit sig_focusIn(); // widget had the focus
}
/*********************************************/
/* focusOutEvent
/*********************************************/
void MyWidget::focusOutEvent( QFocusEvent *event )
{
Q_UNUSED(event);
emit sig_focusOut(); // focus is gone
}The signals are connected to a slot that's part of mainwindow class. My code works but I'm not sure if is the best way to achieve what I would.
For example I read about a virtual bool eventFilter(QObject *obj, QEvent *evt); that can filter ( as it says ) the event sent to other widgets. For example intercepting the focus event. The problem is how can I know it the focus is for ww1 or ww2?
I hope to get a reply.
Best Regards,
Franco