But can it be solved using only standard elements? (i.e. only QTableWidgetItem with the Qt :: ItemIsEditable flag)
Not really. In Qt4, a QTableWidget leaks KeyRelease events from the cell editor, but using this would be an ugly hack.
Maybe you can fix broken edit fields after calling grabKeyboard () on the table?
I tried this somehow and then dispatched events to a QTableWidget , but ran into a problem.
Actually, you need to create your own delegate and set the event filter in createEditor . You can do something like this:
class FilterDelegate : public QStyledItemDelegate { public: FilterDelegate(QObject *filter, QObject *parent = 0) : QStyledItemDelegate(parent), filter(filter) { } virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { QWidget *editor = QStyledItemDelegate::createEditor(parent, option, index); editor->installEventFilter(filter); return editor; } private: QObject *filter; };
Then your MainWindow constructor will look something like this:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { setupUi(this); tableWidget->setItemDelegate(new FilterDelegate(this)); tableWidget->installEventFilter(this); }
And your event filter:
bool MainWindow::eventFilter(QObject *obj, QEvent *event) { if(event->type() == QEvent::KeyPress) { // do something } return QMainWindow::eventFilter(obj, event); }
source share