Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTabBar and dynamic setTabButton



  • Hi all,

    I have a problem with the following function: QTabBar::setTabButton

    The function itself can receive a QWidget.

    I've used my own derived QWidget, which contains two QPushButtons which have a fixed width.

    Theses two buttons are changing their visibility during runtime.

    My problem is, that the tab width doesn't adjust its size dynamically to the new width of the widget.
    I tried to overwrite the sizeHint of the derived widget, which didn't had any effect. At least I used setFixedWidth for the widget. Furthermore I used QTabBar::repaint

    The tab itself doesn't shrink or grow based on the new width of the widget - ONLY the text of the tab is moved based on the widget size.

    If I resize the entire window for 1 pixel all tabs are receiving their correct size based on their widgets.

    Does anyone have a clue how to force the QTabBar to paint the tabs like the entire window was resized?

    Best regards,
    Mike



  • Hi all,

    after a bit trial and error I discovered three dirty work-a-rounds.

    I think the not executed "d->layoutTabs();" is responsible for this behaviour:

    void QTabBar::resizeEvent(QResizeEvent *)
    {
        Q_D(QTabBar);
        if (d->layoutDirty)
            updateGeometry();
        d->layoutTabs();
    
        d->makeVisible(d->currentIndex);
    }
    

    The first work-a-round is to resize the tabBar two times:

        this->tabBar()->resize(this->tabBar()->size() + QSize(1, 1));
        this->tabBar()->resize(this->tabBar()->size() - QSize(1, 1));
    

    Another way would be to set the same icon size again to set the layoutDirty=true:

    this->tabBar()->setIconSize( this->tabBar()->iconSize() );
    

    The problem with this is, that the tabBar jumps to the beginning of the available tabs and doesn't stay at the selected tab.

    The third and maybe best work-a-round is the following:

        if( this->count() > 0 )
            this->tabBar()->setTabText( 0, this->tabBar()->tabText( 0 ) );
    

    Any other ideas are appreciated.

    Best regards,
    Mike


  • Moderators

    @Lachrymology
    try:

    QEvent e(QEvent::FontChange);
    QCoreApplication::sendEvent( tabBar, &e );
    


  • @raven-worx
    Thanks for your work-a-round. It's quite simple and smooth.


Log in to reply