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. Showing the close button for a tab when hovering over the tab
QtWS25 Last Chance

Showing the close button for a tab when hovering over the tab

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.7k Views
  • 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.
  • F Offline
    F Offline
    Fuchsiaff
    wrote on 28 Sept 2018, 12:41 last edited by
    #1

    I have a QTabWidget which has setTabsCloseable(true); and i have a signal connected to it so it would close the tab.
    But now I'm wondering how do I hide the button when the cursor isn't on the TabBar and how to show it when it is on the tab bar?

    R 1 Reply Last reply 28 Sept 2018, 13:30
    0
    • F Fuchsiaff
      28 Sept 2018, 12:41

      I have a QTabWidget which has setTabsCloseable(true); and i have a signal connected to it so it would close the tab.
      But now I'm wondering how do I hide the button when the cursor isn't on the TabBar and how to show it when it is on the tab bar?

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 28 Sept 2018, 13:30 last edited by
      #2

      @Fuchsiaff
      Something like this (UNTESTED):

      class MyTabButton : public QPushButton
      {
      	Q_OBJECT
      
      public:
          MyTabButton(QTabBar* tabBar)
      		: QPushButton(tabbar), m_TabBar(tabbar)
          {
      		m_TabBar->installEventFilter( this );
      		m_OpacityEffect = new QGraphicsOpacityEffect( this );
      			m_OpacityEffect->setOpacity( 0.0 );
      		this->setGraphicsEffect( m_OpacityEffect );
      		
      		connect( this, &MyTabButton::clicked, this, [this]{
      			QMetaObject::invokeMethod(m_TabBar, "tabCloseRequested", Qt::DirectConnection, Q_ARG(int,this->tabIndex())); // trigger tabCloseRequested(int) signal of tabbar. Alternatively trigger a custom signal and react to that
      		});
      		
      		// INIT YOUR BUTTON PROPERTIES (FIXED SIZE, ICON, ETC.) HERE
      	}
      	
      	virtual bool eventFilter(QObject* watched, QEvent* event)
      	{
      		if( watched == m_TabBar )
      		{
      			switch( event->type() )
      			{
      				case QEvent::HoverMove:
      				case QEvent::MouseMove:
      				{
      					QPoint pos = event->type() == QEvent::HoverMove ? static_cast<QHoverEvent*>(event)->pos() : static_cast<QMouseEvent*>(event)->pos();
      					int indexUnderMouse = m_TabBar->tabAt( pos );
      					m_OpacityEffect->setOpacity( indexUnderMouse == this->tabIndex() ? 1.0 : 0.0 );
      				}
      				break;
      				case QEvent::WindowDeactivate:
      				case QEvent::Leave:
      				case QEvent::HoverLeave:
      				{
      					m_OpacityEffect->setOpacity( 0.0 );
      				}
      				break;
      			}
      		}
      		return QPushButton::eventFilter( watched, event );
      	}
      	
      private:
      	int tabIndex() const {
      		for( int i = 0; i < m_TabBar->count(); ++i )
      			if( m_TabBar->tabButton(i,QTabBar::RightSide) == this )
      				return i;
      		return -1;
      	}
      
      	QTabBar*				m_TabBar;
      	QGraphicsOpacityEffect*	m_OpacityEffect;
      };
      
      tabBar->setMouseTracking(true); // might be also work without this
      
      // assign the button to all your closable tabs
      tabbar->setTabButton(index, QTabBar::RightSide, new MyTabButton(tabbar));
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      J 1 Reply Last reply 28 Sept 2018, 15:03
      3
      • R raven-worx
        28 Sept 2018, 13:30

        @Fuchsiaff
        Something like this (UNTESTED):

        class MyTabButton : public QPushButton
        {
        	Q_OBJECT
        
        public:
            MyTabButton(QTabBar* tabBar)
        		: QPushButton(tabbar), m_TabBar(tabbar)
            {
        		m_TabBar->installEventFilter( this );
        		m_OpacityEffect = new QGraphicsOpacityEffect( this );
        			m_OpacityEffect->setOpacity( 0.0 );
        		this->setGraphicsEffect( m_OpacityEffect );
        		
        		connect( this, &MyTabButton::clicked, this, [this]{
        			QMetaObject::invokeMethod(m_TabBar, "tabCloseRequested", Qt::DirectConnection, Q_ARG(int,this->tabIndex())); // trigger tabCloseRequested(int) signal of tabbar. Alternatively trigger a custom signal and react to that
        		});
        		
        		// INIT YOUR BUTTON PROPERTIES (FIXED SIZE, ICON, ETC.) HERE
        	}
        	
        	virtual bool eventFilter(QObject* watched, QEvent* event)
        	{
        		if( watched == m_TabBar )
        		{
        			switch( event->type() )
        			{
        				case QEvent::HoverMove:
        				case QEvent::MouseMove:
        				{
        					QPoint pos = event->type() == QEvent::HoverMove ? static_cast<QHoverEvent*>(event)->pos() : static_cast<QMouseEvent*>(event)->pos();
        					int indexUnderMouse = m_TabBar->tabAt( pos );
        					m_OpacityEffect->setOpacity( indexUnderMouse == this->tabIndex() ? 1.0 : 0.0 );
        				}
        				break;
        				case QEvent::WindowDeactivate:
        				case QEvent::Leave:
        				case QEvent::HoverLeave:
        				{
        					m_OpacityEffect->setOpacity( 0.0 );
        				}
        				break;
        			}
        		}
        		return QPushButton::eventFilter( watched, event );
        	}
        	
        private:
        	int tabIndex() const {
        		for( int i = 0; i < m_TabBar->count(); ++i )
        			if( m_TabBar->tabButton(i,QTabBar::RightSide) == this )
        				return i;
        		return -1;
        	}
        
        	QTabBar*				m_TabBar;
        	QGraphicsOpacityEffect*	m_OpacityEffect;
        };
        
        tabBar->setMouseTracking(true); // might be also work without this
        
        // assign the button to all your closable tabs
        tabbar->setTabButton(index, QTabBar::RightSide, new MyTabButton(tabbar));
        
        J Offline
        J Offline
        JonB
        wrote on 28 Sept 2018, 15:03 last edited by
        #3

        @raven-worx
        Just a question: could most of this be done via a QSS stylesheet :hover etc.?

        R 1 Reply Last reply 29 Sept 2018, 17:13
        0
        • J JonB
          28 Sept 2018, 15:03

          @raven-worx
          Just a question: could most of this be done via a QSS stylesheet :hover etc.?

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 29 Sept 2018, 17:13 last edited by raven-worx
          #4

          @JonB
          i believe thats not possible. (Styling QTabBar)

          Close buttons can be basically styled:

          QTabBar::close-button {
              image: url(close.png)
              subcontrol-position: left;
          }
          QTabBar::close-button:hover {
              image: url(close-hover.png)
          }
          

          But there is no valid QSS selector to style a close button of a (hovered or even specific) tab:

          QTabBar::tab::close-button {} //sub control selectors must always be placed next to an element (next to another sub.control is not allowed)
          QTabBar::tab::close-button:hover {} // even if that would be valid, the :hover pseudo-state would refer to the button not the tab
          QTabBar::tab:hover::close-button {} // pseudo states must appear at the end of the selector
          

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          2

          2/4

          28 Sept 2018, 13:30

          • Login

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