Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Walk through QTableView rows using tab key

Tags:

c++

qt

qtableview

I have a QTableView and i wanted the user to be able to select a whole row rather than individual cell. So i changed the selection behavior as shown below.

table->setSelectionBehavior(QAbstractItemView::SelectRows)

But now when the tab key is clicked it still walks through individual cells rather than whole row. I want the user to be able to walk through each row rather individual cells.

like image 755
Gadheyan .t.s Avatar asked May 14 '26 00:05

Gadheyan .t.s


1 Answers

You must inherit from QTableView class and override keyPressEvent(). For example:

#include <QTableView>
#include <QKeyEvent>

class CustomView : public QTableView
{
    Q_OBJECT

    // QWidget interface
protected:
    void keyPressEvent(QKeyEvent *event) {
        switch(event->key()) {
        case Qt::Key_Tab: {
            if(currentIndex().row() != model()->rowCount())
                selectRow(currentIndex().row() + 1);
            break;
        }
        default: QTableView::keyPressEvent(event);
        }
    }

public:
    explicit CustomView(QWidget *parent = 0);
    ~CustomView(){}

signals:

public slots:

};
like image 158
Shtol Krakov Avatar answered May 15 '26 13:05

Shtol Krakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!