How can I catch if the user edits a QTableWidgetItem
and aborts it via pressing ESC
? I process the content of the respective item by catching QTableWidget::cellDoubleClicked
, and I know when something has been changed by listening to QTableWidget::cellChanged
. But when the user presses ESC
, I don't know it.
I tried to install an event filter on the QTableWidget
, but it only catches key presses that the widget itself receives – the editor seems to be another thing.
Any help would be greatly appreciated!
You must use a delegate and use your eventFilter, the following code is an example:
#include <QApplication>
#include <QKeyEvent>
#include <QStyledItemDelegate>
#include <QTableWidget>
#include <QDebug>
class TableWidgetDelegate: public QStyledItemDelegate{
public:
using QStyledItemDelegate::QStyledItemDelegate;
protected:
bool eventFilter(QObject * object, QEvent * event){
QWidget *editor = qobject_cast<QWidget*>(object);
if(editor && event->type() == QEvent::KeyPress) {
if(static_cast<QKeyEvent *>(event)->key() == Qt::Key_Escape){
qDebug()<<"escape";
}
}
return QStyledItemDelegate::eventFilter(editor, event);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTableWidget w(6, 4);
w.setItemDelegate(new TableWidgetDelegate);
w.show();
return a.exec();
}