Results 1 to 2 of 2

Thread: keyPressEvent() not working for a tab within QTabWidget

  1. #1
    Join Date
    Mar 2008
    Location
    Colorado, USA
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default keyPressEvent() not working for a tab within QTabWidget

    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 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: keyPressEvent() not working for a tab within QTabWidget

    Your QwtPlot is probably eating the key events. Try implementing an event filter method in your QtPlotTab class (see QObject::eventFilter()) and installing it on the QwtPlot instance (QObject::installEventFilter()), and look for the key events there.

    If you do that, be sure to pass the other events along if you aren't handling them. See the installEventFilter() docs for an example of how to do it correctly.

    I also notice that you aren't initializing the base class of QtPlotsTab in the constructor. This could be part of your problem if the event loop isn't being set up properly for your class instance.

Similar Threads

  1. some keys are not working(keyPressEvent)
    By sanjeet in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 12th January 2018, 22:38
  2. QTabWidget QSS transparency not working under Windows
    By il_gatto in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2011, 13:02
  3. keyPressevent not working with timer.
    By T1001 in forum Newbie
    Replies: 5
    Last Post: 9th December 2010, 05:55
  4. Replies: 1
    Last Post: 6th December 2010, 16:18
  5. vertical keypressevent not working on graphicsitem
    By creatio.x in forum Qt Programming
    Replies: 3
    Last Post: 25th December 2009, 12:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.