PDA

View Full Version : Switch the focus between 2 widgets



franco.amato
14th December 2009, 21:07
Hi to all,
In my application I have mainly 2 widgets ( QWidgets , 2 waveform viewers ) and I would switch the focus between these 2 widgets with the shift key.
For example I have
wave_1 and wave_2. When the application starts the wave_1 has focus.
If I press shift wave_2 must have the focus. Pressing shift again wave_1 will have the focus and so on.
Pressing shift the focus must pass from wave_1 and wave_2, no other widgets.


How can I do it?

Best Regards

Lykurg
14th December 2009, 21:54
in wave_1 hold a pointer to wave_2 and vice versa. Then use QWidget::keyPressEvent() and there callwave_X->setFocus().

Or use signal and slots instead of the pointers.

franco.amato
14th December 2009, 22:17
in wave_1 hold a pointer to wave_2 and vice versa. Then use QWidget::keyPressEvent() and there callwave_X->setFocus().

Or use signal and slots instead of the pointers.

Hi, I have a CentralWidget containing the 2 widget.
I don't have to call setFocus from such container widget?

I wrote this code


void CentralWidget::keyPressEvent( QKeyEvent*pe )
{
switch( pe->key() )
{
case Qt::Key_Tab:
{
// switch the focus
if( m_pWave2->hasFocus() )
{
m_pWave1->setFocus( Qt::TabFocusReason );
}
else if ( m_pWave1->hasFocus() )
{
m_pWave2->setFocus(Qt::TabFocusReason);
}

break;
}

default:
QWidget::keyPressEvent(pe);
break;
}
}

this can be ok?

Lykurg
14th December 2009, 22:49
Hi, I have a CentralWidget containing the 2 widget.
I don't have to call setFocus from such container widget?

if you have a main widget, then it's easier.


I wrote this code

...

this can be ok?

That looks nice. Should work. Just try it.

franco.amato
15th December 2009, 01:05
if you have a main widget, then it's easier..

Hi I think I did a mistake.
If I press "tab key" from the main widget it mean that the main widget has the focus to receive keyboard event right?
Is not I want. I neet the focus only between wave_1 and wave_2

so I don't know how to "solve" that problem