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. Discover certain QWidget gained focus in QTabWidget

Discover certain QWidget gained focus in QTabWidget

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 486 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.
  • R Offline
    R Offline
    RotflCopter
    wrote on last edited by RotflCopter
    #1

    Hi

    I am working on simple application with a few tabs, each containing another feature and I added a few QWidget based objects to QTabWidget, I want to discover that focus was gained or lost in certain tab. I wish class QWidget have a signal like focusChanged(bool isFocused) and I could very simply implement certain actions on focus gained/lost inside those QWidget based classes, but there is no such a signal so I made an workaround: each tab has its own tab number passed in constructor and QTabWidget::currentChanged is connected to a slot which checks if currently chosen tab number is equal to its own tab number. It works, but I would love to get rid of it since I have other bloat code to take care of. Are there more elegant ways to catch that focus was gained?

    int tabNumber = 1;
    	
    someFencyTab = new SomeFencyTab(tabNumber++, &someNiceObject, this);
    this->tabWidget->addTab(someFencyTab, "Fency Tab");
    connect(tabWidget, &QTabWidget::currentChanged, someFencyTab, &SomeFencyTab::tabChanged);
    
    void SomeFencyTab::tabChanged(int tab_number)
    {
        bool const now_focused = tab_number == own_tab_number_;
    
        if (now_focused) {
            onFocusGained();
            prev_focused_ = true;
        }
        else if ((!now_focused) && prev_focused_) {
            onFocusLost();
            prev_focused_ = false;
        }
    }
    

    Reimplementing focusInEvent does not work, I tried following

    class SomeFancyTab : public QWidget
    {
    /* ... */
      protected:
        void focusInEvent(QFocusEvent* e);
    }
    
    void SomeFancyTab::focusInEvent(QFocusEvent* e)
    {
        qDebug() << "Focus gained";
    }
    

    Best regards

    Pl45m4P 1 Reply Last reply
    0
    • R RotflCopter

      Hi

      I am working on simple application with a few tabs, each containing another feature and I added a few QWidget based objects to QTabWidget, I want to discover that focus was gained or lost in certain tab. I wish class QWidget have a signal like focusChanged(bool isFocused) and I could very simply implement certain actions on focus gained/lost inside those QWidget based classes, but there is no such a signal so I made an workaround: each tab has its own tab number passed in constructor and QTabWidget::currentChanged is connected to a slot which checks if currently chosen tab number is equal to its own tab number. It works, but I would love to get rid of it since I have other bloat code to take care of. Are there more elegant ways to catch that focus was gained?

      int tabNumber = 1;
      	
      someFencyTab = new SomeFencyTab(tabNumber++, &someNiceObject, this);
      this->tabWidget->addTab(someFencyTab, "Fency Tab");
      connect(tabWidget, &QTabWidget::currentChanged, someFencyTab, &SomeFencyTab::tabChanged);
      
      void SomeFencyTab::tabChanged(int tab_number)
      {
          bool const now_focused = tab_number == own_tab_number_;
      
          if (now_focused) {
              onFocusGained();
              prev_focused_ = true;
          }
          else if ((!now_focused) && prev_focused_) {
              onFocusLost();
              prev_focused_ = false;
          }
      }
      

      Reimplementing focusInEvent does not work, I tried following

      class SomeFancyTab : public QWidget
      {
      /* ... */
        protected:
          void focusInEvent(QFocusEvent* e);
      }
      
      void SomeFancyTab::focusInEvent(QFocusEvent* e)
      {
          qDebug() << "Focus gained";
      }
      

      Best regards

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @RotflCopter said in Discover certain QWidget gained focus in QTabWidget:

      I wish class QWidget have a signal like focusChanged(bool isFocused) and I could very simply implement certain actions on focus gained/lost inside those QWidget

      What about QApplication::focusWidget?

      • https://doc.qt.io/qt-5/qapplication.html#focusWidget

      A QWidget is still a QWidget even when it's part of a tab.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      1
      • R Offline
        R Offline
        RotflCopter
        wrote on last edited by
        #3

        Hi

        As usual, after posting topic I found solution:
        -reimplementing QObject general event handler to see what kinds of event comes when certain action happens (switching to tab)

        bool event(QEvent *event);
        

        -reimplementing specific handlers - in my case:

        void hideEvent(QHideEvent *event);
        void showEvent(QShowEvent *event);
        

        Best regards
        Hope this topic will help someone in future

        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