View Full Version : QTableView key pressed

20th October 2007, 13:59
In my project I use a QTableView and I want to delete the current row when the user press del. I want to catch the signal keyPressEvent(QKeyEvent*) of the tableview but it does not work do you know why? I used :

connect(ui.categoriesTree,SIGNAL(keyPressEvent(QKe yEvent*)),this,SLOT(FilesKeyPressEvent(QKeyEvent*) ));

and I don’t get the function executed. Thanks a lot.

20th October 2007, 14:31
That's an event, not a signal. You have to subclass QTableView and override it.

20th October 2007, 16:59
Is there any other way to delete a row when the user press del without sub classing QTableView ?

20th October 2007, 17:05
You could use QShortcut:

QShortcut* shortcut = new QShortcut(QKeySequence(QKeySequence::Delete), tableView);
connect(shortcut, SIGNAL(activated()), receiver, SLOT(deleteRow()));

void Receiver::deleteRow()
QModelIndex idx = tableView->currentIndex();
if (idx.isValid())
tableView->model()->removeRow(idx.row(), idx.parent());

20th October 2007, 17:09
Thanks. I think that this will work.

29th July 2013, 05:13
Hi JP-N,
I am trying a similar thing with the Escape key in a QTableView. I am using the QShortcut, tied to the qtableview and then connecting the activated signal.

What happens is that it works fine as long as I am not editing a row in the qtableview, if data entry is occurring in the table, the first "Esc" key sequence resets the table to the pre-edit state, and the second and subsequent presses catch fine.

My question is how can I intercept the first ESC key sequence, utilizing the QShortcut?

keyEsc = new QShortcut(QKeySequence(Qt::Key_Escape),ui->tableView);
connect(keyEsc,SIGNAL(activated()),this,SLOT(ESCPr essed_tblview()));


1st August 2013, 04:20
For anyone interested, this did work but required first re-implementing the eventFilter in a lineEditDelegate class based on qItemDelegate. Please see this (http://www.qtcentre.org/threads/55496-Simple-QT-SQL-Viewer-Updater-Revisited)for additional information on this solution!