Results 1 to 8 of 8

Thread: QTableWidget + setCellWidget - controlling tab order

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableWidget + setCellWidget - controlling tab order

    I've been trying for some time now to make QTableWidget behave the way I want it.
    I have a few MyTableWidgets (which inherit from QTableWidget) on the one tab. I modified QTableWidget so it would display in the first (and as for now only) row QLineEdits (to be precise it displays my class inheriting from QLineEdit) using QTableWidget::setCellWidget().
    Now I would like to traverse through all MyTableWidgets on this tab.
    If I have only a one column (so it's also the only cell in MyTableWidget) when I press Tab it goes to the next MyTableWidget (and I've reimplemented focusInEvent so the first cell in this MyTableWidget gets the focus). When I press Tab again and again it loops through all cells in the only row in this MyTableWidget, but never leaves MyTableWidget and I don't know where to change this behavior.
    So my question is how to change QTableWidget so that after pressing Tab in the last cell it will go to the next widget not first cell (and shift+tab in the first cell to go to the previous widget)?

    From all I read it appears I should do something by reimplementing focusNextPrevChild( bool), but I just can't get it to work properly.

    edit:
    I think I found the solution:
    Qt Code:
    1. bool MyTableWidget::focusNextPrevChild( bool next )
    2. {
    3. std::cout << "focusNextPrev " << qPrintable(objectName()) << std::endl;
    4. if( next )
    5. {
    6. if( currentColumn() == m_model->columnCount() - 1 )
    7. return QWidget::focusNextPrevChild(next);
    8. return false;
    9. }
    10. else
    11. {
    12. if( currentColumn() == 0 )
    13. return QWidget::focusNextPrevChild(next);
    14. return false;
    15. }
    16.  
    17.  
    18. return QTableWidget::focusNextPrevChild(next);
    19. }
    20.  
    21.  
    22. void MyTableWidget::focusInEvent( QFocusEvent * e)
    23. {
    24. switch( e->reason() )
    25. {
    26. case Qt::BacktabFocusReason:
    27. cellWidget(0, m_model->columns().count()-1)->setFocus();
    28. break;
    29. case Qt::TabFocusReason:
    30. default:
    31. cellWidget(0,0)->setFocus();
    32. break;
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 
    As far as I checked it does work, though I'm not sure it's entirely correct.
    Last edited by elmo; 13th September 2009 at 14:00.

Similar Threads

  1. set Tab Order in a QTableWidget
    By jfe in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2008, 12:55
  2. Replies: 10
    Last Post: 9th July 2007, 22:02
  3. Replies: 1
    Last Post: 26th February 2006, 05:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.