Another attempt:

MyTable.h:
Qt Code:
  1. #include <QTableWidget>
  2. #include <QFocusEvent>
  3. #include <QEvent>
  4. #include <QLineEdit>
  5.  
  6. #include <iostream>
  7.  
  8. class MyTable : public QTableWidget
  9. {
  10. Q_OBJECT
  11.  
  12. public:
  13. MyTable( QWidget * parent = 0) : QTableWidget(parent)
  14. {
  15. setColumnCount(7);
  16. setRowCount(1);
  17.  
  18. for(unsigned int i = 0; i < columnCount(); ++i)
  19. {
  20. QWidget * e = new QLineEdit(this);
  21. setCellWidget(0, i, e);
  22. e->setFocusPolicy( Qt::ClickFocus );
  23. }
  24. }
  25.  
  26. bool event( QEvent * e )
  27. {
  28. switch( e->type() )
  29. {
  30. case QEvent::KeyPress:
  31. e->accept();
  32. QKeyEvent *ke = (QKeyEvent *) e;
  33. QObject *p = parent();
  34. bool res = true;
  35.  
  36. switch( ke->key() )
  37. {
  38. case Qt::Key_Backtab:
  39. res = focusNextPrevChild(false);
  40. break;
  41. case Qt::Key_Tab:
  42. res = focusNextPrevChild(true);
  43. break;
  44. }
  45. return res;
  46. }
  47.  
  48. return QTableWidget::event(e);
  49. }
  50.  
  51. void focusInEvent( QFocusEvent *e )
  52. {
  53. switch( e->reason() )
  54. {
  55. case Qt::BacktabFocusReason:
  56. setCurrentCell(0, columnCount()-1);
  57. break;
  58. case Qt::TabFocusReason:
  59. default:
  60. setCurrentCell(0,0);
  61. break;
  62. }
  63. }
  64.  
  65. bool focusNextPrevChild( bool next )
  66. {
  67. if( next )
  68. {
  69. if( currentColumn() < columnCount() - 1 )
  70. {
  71. setCurrentCell( 0, currentColumn() + 1);
  72. return true;
  73. }
  74. }
  75. else if( currentColumn() > 0 )
  76. {
  77. setCurrentCell( 0, currentColumn() - 1);
  78. return true;
  79. }
  80.  
  81. return false;
  82. }
  83.  
  84. };
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?