Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTabWidget::event() override is never called
Forum Updated to NodeBB v4.3 + New Features

QTabWidget::event() override is never called

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mayaknife
    wrote on last edited by mayaknife
    #1

    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
    {
        Q_OBJECT
    
    public:
        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::~ViewTabs()
    {
    }
    
    bool
    ViewTabs::event(QEvent *e)
    {
        std::cout << "ViewTabs::event() event" << std::endl;
    
        return QTabWidget::event(e);
    }
    
    
    void
    ViewTabs::mousePressEvent(QMouseEvent *event)
    {
        Q_UNUSED(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.

    joeQJ 1 Reply Last reply
    0
    • M mayaknife

      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
      {
          Q_OBJECT
      
      public:
          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::~ViewTabs()
      {
      }
      
      bool
      ViewTabs::event(QEvent *e)
      {
          std::cout << "ViewTabs::event() event" << std::endl;
      
          return QTabWidget::event(e);
      }
      
      
      void
      ViewTabs::mousePressEvent(QMouseEvent *event)
      {
          Q_UNUSED(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.

      joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by
      #2

      @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
      {
      public:
          TabWidget(QWidget* parent = 0);
      
      protected:
          void mousePressEvent(QMouseEvent *event) override;
      };
      
      #endif // TABWIDGET_H
      
      

      cpp file

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

      Just do it!

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi

        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

        joeQJ 1 Reply Last reply
        4
        • mrjjM mrjj

          Hi

          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

          joeQJ Offline
          joeQJ Offline
          joeQ
          wrote on last edited by
          #4

          @mrjj

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

          Just do it!

          mrjjM 1 Reply Last reply
          0
          • joeQJ joeQ

            @mrjj

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @joeQ
            Well i have been surprised a few times finding a feature that really rocks.
            Like
            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 :)

            joeQJ 1 Reply Last reply
            1
            • mrjjM mrjj

              @joeQ
              Well i have been surprised a few times finding a feature that really rocks.
              Like
              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 :)

              joeQJ Offline
              joeQJ Offline
              joeQ
              wrote on last edited by
              #6

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

              Just do it!

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved