Congratulations to our 2022 Qt Champions!

How to get a signal for QTableView cell selection by cursor?

  • Hello,

    I'm using Qt 5.5.1 within an appliaction that present QSqlDatabaseModel data in a QTableView. Displaing the data works well but I need a feature that I don't know how to implement it. What I need is to know which row/cell is selected. This is working well for cells ativated by clicking with the mouse by handling the click signal. It's also working when I move the cursor with the arrow keys to another cell an pressing enter. That will give an activated signal which I can handle.

    What I'm missing is some signal that let me know in which cell the cursors is when I'm moving the selection only with the arrow keys without pressing enter. By using only the arrow keys the selection is moving but no signal is fired.

    is there a way to change the selection mode on that way that also cursor/selction movements done with arrow keys only are signaled?

    Thanks & best regards Michael

  • The signal you are looking for is QItemSelectionModel::selectionChanged from tableView->selectionModel()

    or currentChanged() depending what you want to capture

  • @kuschky

    Are you using signals of QTableView?
    Which ones?

    I guess you are already using the signals of QTableWidget which inherits QTableView.
    Did you see cellEntered of QTableWidget

  • Hello,

    I got solved it. Signal selectionChanged is only fired after moving with the cursor keys to the table cell and pressing enter. But I don't want to have to press enter.

    The solution was derive my own TableView class from QTableView

    class GenericTableView : public QTableView
        GenericTableView(QObject* parent = NULL);
        void currentChanged(const QModelIndex &current, const QModelIndex &previous);
        void currentChangedSignal(QModelIndex, QModelIndex);

    and overwrite currentchanged and sending a self defined signal where I react on

    void GenericTableView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
        emit currentChangedSignal(current, previous);

    Next problem was to use this derived class in the Qt From Designer. To solve it first you have to insert a normal QTableView and then do a right click on it and click "promote to" here you can specify that the element refers to yout own (in my case GenericTableView) TableView. And last but not least do another right right click and select "Change signals/slots" here you can add your self defined signal (in my case currentChangedSignal).

    Now you can connect the signal with a slot on the common way by right cklick and select "Goto slot". Thats it.

    Thanks everbody for your support.

Log in to reply