Another attempt:
MyTable.h:
#include <QTableWidget>
#include <QFocusEvent>
#include <QEvent>
#include <QLineEdit>
#include <iostream>
{
Q_OBJECT
public:
{
setColumnCount(7);
setRowCount(1);
for(unsigned int i = 0; i < columnCount(); ++i)
{
setCellWidget(0, i, e);
e->setFocusPolicy( Qt::ClickFocus );
}
}
{
switch( e->type() )
{
e->accept();
bool res = true;
switch( ke->key() )
{
case Qt::Key_Backtab:
res = focusNextPrevChild(false);
break;
case Qt::Key_Tab:
res = focusNextPrevChild(true);
break;
}
return res;
}
}
{
switch( e->reason() )
{
case Qt::BacktabFocusReason:
setCurrentCell(0, columnCount()-1);
break;
case Qt::TabFocusReason:
default:
setCurrentCell(0,0);
break;
}
}
bool focusNextPrevChild( bool next )
{
if( next )
{
if( currentColumn() < columnCount() - 1 )
{
setCurrentCell( 0, currentColumn() + 1);
return true;
}
}
else if( currentColumn() > 0 )
{
setCurrentCell( 0, currentColumn() - 1);
return true;
}
return false;
}
};
#include <QTableWidget>
#include <QFocusEvent>
#include <QEvent>
#include <QLineEdit>
#include <iostream>
class MyTable : public QTableWidget
{
Q_OBJECT
public:
MyTable( QWidget * parent = 0) : QTableWidget(parent)
{
setColumnCount(7);
setRowCount(1);
for(unsigned int i = 0; i < columnCount(); ++i)
{
QWidget * e = new QLineEdit(this);
setCellWidget(0, i, e);
e->setFocusPolicy( Qt::ClickFocus );
}
}
bool event( QEvent * e )
{
switch( e->type() )
{
case QEvent::KeyPress:
e->accept();
QKeyEvent *ke = (QKeyEvent *) e;
QObject *p = parent();
bool res = true;
switch( ke->key() )
{
case Qt::Key_Backtab:
res = focusNextPrevChild(false);
break;
case Qt::Key_Tab:
res = focusNextPrevChild(true);
break;
}
return res;
}
return QTableWidget::event(e);
}
void focusInEvent( QFocusEvent *e )
{
switch( e->reason() )
{
case Qt::BacktabFocusReason:
setCurrentCell(0, columnCount()-1);
break;
case Qt::TabFocusReason:
default:
setCurrentCell(0,0);
break;
}
}
bool focusNextPrevChild( bool next )
{
if( next )
{
if( currentColumn() < columnCount() - 1 )
{
setCurrentCell( 0, currentColumn() + 1);
return true;
}
}
else if( currentColumn() > 0 )
{
setCurrentCell( 0, currentColumn() - 1);
return true;
}
return false;
}
};
To copy to clipboard, switch view to plain text mode
I don't know why this works only one way: when pressing tab it loops as it should. But when I press Shift+Tab ( = Key_Backtab) it loops inside one MyTable. Why does it behave like that?
Bookmarks