4

I have QDialog that contains few buttons and a QTextEdit. after writing something in the QTextEdit, I press tab in order to get to one of the buttons, but when I pressing tab, a tab space is added to the QTextEdit. How can I change this behavior?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
kakush
  • 3,334
  • 14
  • 47
  • 68

2 Answers2

15

You can use setTabChangesFocus method of QTextEdit:

yourTextEdit.setTabChangesFocus(true);
Kirill Chernikov
  • 1,387
  • 8
  • 21
2

You can subclass QTextEdit and override the keyPressEvent to intercept the tab key. Then, use nextInFocusChain to determine the next focus widget and call setFocus on it

Outline:

class MyTextEdit : public QTextEdit
{
public:
    MyTextEdit(QWidget *parent = 0) : QTextEdit(parent) {}

protected:
    void keyPressEvent(QKeyEvent *e) {
        if (e->key() == Qt::Key_Tab) {
            nextInFocusChain()->setFocus(Qt::TabFocusReason);
        } else {
            QTextEdit::keyPressEvent(e);
        }
    }
};
king_nak
  • 11,313
  • 33
  • 58