Qt-interest Archive, March 2002
Tabbing out of a QTextEditor
Message 1 in thread
Hello,
I am using a QTextEditor sub-class in a widget along with several other
controls. Tabs in the text are not to the text that will be used in the
sub-class and I'd like to be able to tab into and out of the QTextEditor
sub-class just as happens with the other controls.
I realise that I need to override the QTextEditor handling of Key_Tab
events so I've given my sub-class a simple eventFilter.
bool MyTextEdit::eventFilter( QObject *o, QEvent *e )
{
switch ( e->type( ) )
{
case QEvent::KeyPress:
{
if ( ke->key( ) == Key_Tab ||
ke->key( ) == Key_BackTab )
{
return (true);
}
}
break ;
default:
;
}
return QTextEdit::eventFilter( o, e );
}
Using that eventFilter I can stop the QTextEditor default handling of
Key_Tab, but it seems I need to do something else to get the tabbing out
of the control to work. Any ideas?
--
[ signature omitted ]
Message 2 in thread
Hi David,
Eventually, you need to know when to stop intercepting the tabs...when the
person should tab out of the widget, call the superclass's eventfilter with
the same args.
This is purely conjecture because I've never done this with a qtextedit widget,
but I have done something extremely similar using keypressevent for a
qlineedit. I imagine it's about the same.
ry
On Tue, Mar 05, 2002 at 06:01:57PM +0000, David Wright wrote:
> Hello,
>
> I am using a QTextEditor sub-class in a widget along with several other
> controls. Tabs in the text are not to the text that will be used in the
> sub-class and I'd like to be able to tab into and out of the QTextEditor
> sub-class just as happens with the other controls.
>
> I realise that I need to override the QTextEditor handling of Key_Tab
> events so I've given my sub-class a simple eventFilter.
>
> bool MyTextEdit::eventFilter( QObject *o, QEvent *e )
> {
> switch ( e->type( ) )
> {
> case QEvent::KeyPress:
> {
> if ( ke->key( ) == Key_Tab ||
> ke->key( ) == Key_BackTab )
> {
> return (true);
> }
> }
> break ;
> default:
> ;
> }
>
> return QTextEdit::eventFilter( o, e );
> }
>
> Using that eventFilter I can stop the QTextEditor default handling of
> Key_Tab, but it seems I need to do something else to get the tabbing out
> of the control to work. Any ideas?
>
> --
> Regards
> David Wright, Skyewright, Elgol, Isle of Skye, IV49 9BL Scotland
> david.wright@skyewright.co.uk
> <URL: http://www.skyewright.co.uk/> Synergy add-ons for Sage
>
> --
> List archive and information: http://qt-interest.trolltech.com
--
[ signature omitted ]
Message 3 in thread
ryan p bobko <ryan@ostrich-emulators.com> wrote:
>Hi David,
>Eventually, you need to know when to stop intercepting the tabs...when the
>person should tab out of the widget, call the superclass's eventfilter with
>the same args.
Thanks Ryan (& Clinton off-list). Between you you've given me enough
ideas to reach the following solution which seems to do what I want.
bool MyTextEdit::eventFilter( QObject *o, QEvent *e )
{
switch ( e->type( ) )
{
case QEvent::KeyPress:
{
QKeyEvent *ke = (QKeyEvent*)e;
if ( ke->key( ) == Key_Tab ||
ke->key( ) == Key_BackTab )
{
if ( ke->key() == Key_Tab && !(ke->state() & ShiftButton))
{
QFocusEvent::setReason( QFocusEvent::Tab );
bool res = QWidget::focusNextPrevChild( TRUE );
QFocusEvent::resetReason();
return (res);
}
else // Key_BackTab or shifted Key_Tab
{
QFocusEvent::setReason( QFocusEvent::Backtab );
bool res = QWidget::focusNextPrevChild( FALSE );
QFocusEvent::resetReason();
return (res);
}
}
} break ;
default:
break ;
}
return QTextEdit::eventFilter( o, e );
}
I may refine it further to allow [Ctrl][Tab] to get through to the
QTextEdit, but in essence I now have the desired behaviour. Thanks.
--
[ signature omitted ]