Important: Please read the Qt Code of Conduct -

QTabWidget::event() override is never called

  • I've subclassed QTabWidget and provided overrides for the event(QEvent*) and mousePressEvent(QMouseEvent*) virtuals, but they are never called.

    I know that I can install an event filter to handle those events, but why don't the overrides work?

    Here's my class declaration:

    class ViewTabs : public QTabWidget
        ViewTabs(QWidget *parent=NULL);
        virtual ~ViewTabs();
        virtual bool event(QEvent *e);
        virtual void mousePressEvent(QMouseEvent *event);

    and the implementation:

    ViewTabs::ViewTabs(QWidget *parent)
    :   QTabWidget(parent)
    ViewTabs::event(QEvent *e)
        std::cout << "ViewTabs::event() event" << std::endl;
        return QTabWidget::event(e);
    ViewTabs::mousePressEvent(QMouseEvent *event)
        std::cout << "ViewTabs press" << std::endl;

    EDIT: The overrides are indeed being called correctly. I had assumed they weren't because print statements placed in them were not appearing on the console. That was because I didn't have the 'console' setting in my CONFIG.

  • @mayaknife hi, friend, welcome

    I tried my Qt and to write test, it can grab mouse click event in subclass QTabWidget

    my snippet code:

    head file

    #ifndef TABWIDGET_H
    #define TABWIDGET_H
    #include <QTabWidget>
    class TabWidget : public QTabWidget
        TabWidget(QWidget* parent = 0);
        void mousePressEvent(QMouseEvent *event) override;
    #endif // TABWIDGET_H

    cpp file

    #include "tabwidget.h"
    #include <QtCore/QDebug>
    TabWidget::TabWidget(QWidget *parent)
    void TabWidget::mousePressEvent(QMouseEvent *event)

  • Lifetime Qt Champion


    I can highly recommend to use c++11 keyword override

    virtual bool event(QEvent *event) override;

    as compiler will then tell you if it overrides nothing and u know something is wrong.

    Also to be sure signature ( return type and parameters) is correct, one
    can use the menu to insert base class overrides.

    Right click the class definition ( on the name ) and go to Refactor menu
    alt text
    then you get nice list

    alt text

  • @mrjj

    Using such a long Qt IDE, have not found this method. Best way to insert virtual function. Thank you very much.

  • Lifetime Qt Champion

    Well i have been surprised a few times finding a feature that really rocks.
    When you add a parameter function and it shows that little lamp in the end.
    Press alt+enter to let it change/add the parameter to other file. (.h or .cpp) by itself.

    In Designer , you can actually drag a widget BACK to the widget list and use it as a template for a given
    widget type. So you don't have to set the properties all over. Composite/complex objects also works

    The Refactor right click menu is context sensitive and offer different
    menus depending on the text cursor location.
    Like standing on a member variable , it offers to create get/set function for it.
    alt text
    (alt + enter also works)

    and few other little surprises :)

  • @mrjj
    so surprised, these ways are very convenient and efficient. thank you very much.

Log in to reply