Hi, everyone!
I wanna ask the solution for the situation:
My project has MainWindow Class and tableView and textEdit
(last two were created through the Designer in mainwindow.ui).
I need to do some action when shortcut pressed in tableView.
I've got right reaction through the
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
To copy to clipboard, switch view to plain text mode
here is the code:
...
// in MainWindow constructor
ui->tableView->installEventFilter(this);
...
{
if(watched
== ui
->tableView
&& event
->type
() == QEvent::KeyPress){ QKeyEvent *keyEvent
= static_cast<QKeyEvent
*>
(event
);
qDebug()<<keyEvent->key();// DO SOMETHING
}
return QWidget::eventFilter(watched, event
);
}
...
// in MainWindow constructor
ui->tableView->installEventFilter(this);
...
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if(watched == ui->tableView && event->type() == QEvent::KeyPress){
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
qDebug()<<keyEvent->key();// DO SOMETHING
}
return QWidget::eventFilter(watched, event);
}
To copy to clipboard, switch view to plain text mode
But maybe there is a simpler solution? How to catch keypress in tableView?
Cause i've created tableView in *.ui i can't simple use keyPressEvent for tableView only for MainWindow (it doesn't recognize tableView or textEdit).
Thank's in advance.
Bookmarks