I am writing an application that will be used on a screen that has some hardware custom buttons (mapped to function key combinations). Each button is used to change the screen displayed, so I thought a stacked widget seems reasonable.
To develop I have created a debug window that that has these buttons as QPushButtons, with the "normal" Mainwindow embedded. When running in release build I was intending to run this as the main window
This all works fine, but key press events are not propagating as I'd expected. Reading the Qt documentation it indicates that the widget with focus would receive the events, so I had assumed the widget shown in the stacked widget would receive the key press but the debug window always receives the key events, regardless of any focus policy I set
Debug Window:
DebugWindow::DebugWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::GVADemo)
{
ui->setupUi(this);
MainWindow* main = new MainWindow(this);
QLayout* layout = new QHBoxLayout;
layout->addWidget(main);
layout->setContentsMargins(0,0,0,0);
ui->frame->setLayout(layout);
}
void GVADemo::keyPressEvent(QKeyEvent *event)
{
qDebug("GVADemo keyPressEvent %d", event->key());
}
Main Window:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
Screen* screen1 = new Screen();
QStackedWidget* stackedWidget = new QStackedWidget(this);
stackedWidget->addWidget(screen1);
stackedWidget->setCurrentWidget(screen1);
setCentralWidget(stackedWidget);
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
qDebug("MainWindow keyPressEvent %d", event->key());
}
Example of screen in stacked widget:
Screen::Screen(QWidget *parent) : QWidget(parent), ui(new Ui::Screen)
{
ui->setupUi(this);
}
void SAScreen::keyPressEvent(QKeyEvent *event)
{
qDebug("Screen keyPressEvent %d", event->key());
}
QEvent::KeyPress
... – Scheff's Cat