PDA

View Full Version : Question about focus



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

Santosh Reddy
14th July 2011, 03:31
bool MyMainWindow::eventFilter(QObject *obj, QEvent *evt)
{
QWidget * w = dynamic_cast<QWidget *>(obj);
if(w == ww1)
return false; // widget 1 event
else if(w == ww2)
return false; // widget 2 event
else
return QMainWindow::eventFilter(obj, event);
}

franco.amato
14th July 2011, 05:47
bool MyMainWindow::eventFilter(QObject *obj, QEvent *evt)
{
QWidget * w = dynamic_cast<QWidget *>(obj);
if(w == ww1)
return false; // widget 1 event
else if(w == ww2)
return false; // widget 2 event
else
return QMainWindow::eventFilter(obj, event);
}

Thank you very much.
Just a couple of things:
Why did you return false in case of ww1 and ww2?
I have to install the eventfilter in my MyWidget class right?

Thanx again

Santosh Reddy
14th July 2011, 05:59
Why did you return false in case of ww1 and ww2?
you can replace it with your functions, and return false, this will ensure that event is sent to the actual widget ww1 / ww2

I have to install the eventfilter in my MyWidget class right?
event filters are installed on objects not on class, you need to install event filter in ww1 and ww2 not on MyWidget

franco.amato
15th July 2011, 07:31
Hi thanx,
in which case I have to return true?
This is what I wrote:


bool MainWindow::eventFilter(QObject *obj, QEvent *evt)
{
WaveWidget *w = dynamic_cast<WaveWidget *>(obj);
if(w == ww1)
{
switch (evt->type())
{
case QEvent::FocusIn:
qDebug("Wave 1 received focus");
return false;
case QEvent::FocusOut:
qDebug("Wave 1 focus lost");
return false;
default:
return true; // In this case I have to return true?
}
}
else if(w == ww2)
{
switch (evt->type())
{
case QEvent::FocusIn:
qDebug("Wave 2 received focus");
return false;
case QEvent::FocusOut:
qDebug("Wave 2 focus lost");
return false;
default:
return true; // In this case I have to return true?
}
}
else
return QMainWindow::eventFilter(obj, evt);
}

My doubt is in when I have to return true?
Regards

Santosh Reddy
15th July 2011, 07:42
if you want filter the event / eat the event / stop the event / consume the event, then return true, by default return QMainWindow::eventFilter(obj, evt);