I have a tab within a QTabWidget. I have a keyPressEvent() override defined in my class managing the tab but the key press events are not recognized (I have a cout call to verify that the function isn't even called).

I have tried activeWindow(), setFocusPolicy(QtFocusPolicy::StrongFocus), and setFocus() both on the tab and the widget itself, but that doesn't seem to help.

When I implement a keyPressEvent() on the main window widget containing the tab widget, it seems to work, but I'd rather do it on the individual tabs if possible.

Code for main window
Qt Code:
  1. QtPlotsMain::QtPlotsMain()
  2. : ui( new Ui::QtPlotsMain() ),
  3. help_ui( new Ui::Help()),
  4. help_dialog(new QDialog())
  5. {
  6. //Setup plot
  7. ui->setupUi( this );
  8. tabWidget = new QTabWidget(ui->centralwidget);
  9. tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
  10. tabWidget->setIconSize(QSize(16, 16));
  11. tabWidget->setTabsClosable(true);
  12. tabWidget->setMovable(true);
  13. QWidget * tab_2 = new QWidget();
  14.  
  15. tabWidget->addTab(tab_2, "Altitude vs Time");
  16.  
  17. ui->gridLayout->addWidget(tabWidget, 0, 0, 1, 1);
  18.  
  19. shared_ptr<QtPlotsTab> alt_tab(make_shared<AltVsTimeTab>(tab_2));
  20. plot_objects.push_back(alt_tab);
To copy to clipboard, switch view to plain text mode 

Code for tab:
Qt Code:
  1. QtPlotsTab::QtPlotsTab(QWidget *tab_in,
  2. const QString & title_in,
  3. const QString & x_label_in,
  4. const QString & y_label_in)
  5. : map_src_curve(make_shared<SensorPlotCollection>())
  6. {
  7. QGridLayout *gridLayout_2 = new QGridLayout(tab_in);
  8. //Setup plot
  9. plot = new QwtPlot(tab_in);
  10. gridLayout_2->addWidget(plot, 0, 0, 1, 1);
  11.  
  12. // have tried a variety of things here
  13. activateWindow();
  14. setFocusPolicy(Qt::FocusPolicy::StrongFocus);
  15. setFocus();
  16.  
  17. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. void QtPlotsTab::keyPressEvent(QKeyEvent *e)
  2. {
  3. cout << FILE_LINE << endl;
  4. }
To copy to clipboard, switch view to plain text mode