Congratulations to our 2022 Qt Champions!

How to control tab focus?

  • Hi,

    I've got a main_window with a table.
    Each table cell has its own QWidget window with different elements.
    In the first column, each cell contains a QTextEdit, every other cell contains QCheckBoxes.
    Now my question:
    How can I control that the focus jumps only from QTextEdit to QTextEdit by typing the TAB on keyboard?
    When QTextEdit in row 1 has focus and I press TAB, focus jumps to QCheckBox in row 1, but I want the focus to jump to QTextEdit in row 2 instead...

    Is there a way to define focus index like in html or something like this? Maybe with a layout? I don't know...

  • Lifetime Qt Champion


    Did you have a look at setTabOrder ?

    Hope it helps

  • and also you can change focus policy as no focus of QCheckBoxes.


  • Hi again,

    first of all thank you for the support!
    I took a look at setTabOrder, but there is something wrong with it.
    I have a QWidget based subclass "main_window" with a table within.
    Each table cell has it's own QWidget based child window "table_cell". This class "table_cell" contains things like QTextEdit, QCheckBox etc.
    I create those child widgets with a loop within a loop (rows & columns):
    @for(int i = 0; i < table->rowCount(); i++)
    for(int j = 0; j < table->columnCount(); j++)
    table_cell* cell; // <-- QWidget class
    if(j == 0)
    QWidget* cell_widget = table->cellWidget(i-1, j);
    table_cell* cell_last = qobject_cast<table_cell*> (cell_widget);
    cell = new table_cell(this, "textedit", "cell", i, j); // <--- creating a table_cell widget with a QTextEdit
    this->setTabOrder(cell_last->textedit, cell->textedit); // <--- trying to use setTabOrder
    Problem is, that since I wrote the setTabOrder command, my program starts but stops executing after 0.5 seconds without displaying the window...
    No compiler errors, just closing the exe file directly after running it...

    What's wrong with my command?

  • I think it's all weird because I use those widgets in a table, right?

    Maybe someone can help me finding a better way to realize the following:

    • Creating a mainwindow with a table and a button next to the table (QHBoxLayout maybe?)
    • table should have 20 rows and 10 columns
    • in column 1, every cell should contain a QTextEdit (full size), the other column cells should contain QCheckBoxes (horizontally & vertically centered --> maybe also QHBoxLayout)
    • QTextEdit in row 1 should have focus on program start
    • Focus should jump from QTextEdit to QTextEdit by typing TAB key
    • on clicking button, it should be possible to get information about all the cell widgets (contents of QTextEdit, checkstate of QCheckBox).

    How do I realize this best?

    Read a bit about QTableWidget TAB management --> tables have it's own tabfocuspolicy which denies using setTabOrder or stuff like that.
    @table->setTabKeyNavigation(false); // disable table's focus management
    textedit->setFocusPolicy(Qt::StrongFocus); // it also works without that line, but I do not understand why...

  • Lifetime Qt Champion

    There's something that's intriguing, since all cells have different widgets. What's the purpose of your QTableWidget ? If you don't use any model a QWidget with a QGridLayout would probably be better

Log in to reply