Pressing Tab in QTextEdit in Dialog - change behavior
Asked Answered
D

2

5

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?

Dermatome answered 25/8, 2016 at 13:20 Comment(0)
M
16

You can use setTabChangesFocus method of QTextEdit:

yourTextEdit.setTabChangesFocus(true);
Marylinmarylinda answered 25/8, 2016 at 13:26 Comment(1)
Works for PyQt5 as well.Marshmallow
P
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);
        }
    }
};
Philosophism answered 25/8, 2016 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.