I have one main Widget
and inside this main widget I have QListWidget
and two buttons. I have override the keyPressEvent
inside the main widget (inherited from QWidget
). I can receive the keyPress
events when focus is not on QListWidget
, but when focus is inside the QListWidget
I am unable to receive these keyPress
events. Below is the code I have used to achieve this:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setFocusPolicy(Qt::StrongFocus);
ui->listWidget->addItem(new QListWidgetItem("Item1"));
ui->listWidget->addItem(new QListWidgetItem("Item2"));
ui->listWidget->addItem(new QListWidgetItem("Item3"));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
qDebug() << "event->key(): " << event->key();
QWidget::keyPressEvent(event);
}