Important: Please read the Qt Code of Conduct -

How to set vertical tab order in a QTableWidget

  • Seems like it should be simple enough, but Qt is not behaving at all how I expect. I have some standard QTableWidget tables and I'm trying to get them to tab vertical first (rather than the default horizontal first). Here is the function I currently have
    void setVerticalTabOrder(QTableWidget *tbl)
    for(int j=0; j<tbl->columnCount(); j++)
    for(int i=0; i<tbl->rowCount()-1; i++)
    QWidget::setTabOrder(tbl->cellWidget(i,j), tbl->cellWidget(i+1,j));
    if(j != tbl->columnCount()-1)
    QWidget::setTabOrder(tbl->cellWidget(tbl->rowCount()-1,j), tbl->cellWidget(0,j+1));
    QWidget::setTabOrder(tbl->cellWidget(tbl->rowCount()-1,j), tbl->cellWidget(0,0));

    Which doesn't seem to change the behavior at all. Searching around I've seen some posts about subclassing QTableWidget and doing custom handling, but I don't see why I need to go through all that effort just to change to vertical first tabbing. More importantly, I want to know why the above doesn't do what I expect.


  • Moderators

    I think that QTableWidget manually handles tab key presses so setting tab order on widgets inside the cells doesn't have any effect.
    If you don't want to subclass, you can install an event handler and catch the QEvent::ShortcutOverride events. Then you check for the Qt::Key_Tab or Qt::Key_Backtab and use setCurrentIndex() to traverse in any custom order.

  • Since the setTabOrder function is a static under the QWidget class, I was under the impression that this functionality should be relatively independent of the QTableWidget class specifically.

  • Moderators

    I didn't look at the actual implementation but "should" might just be the keyword here :)

Log in to reply