Search code examples
qttabsqt5qtabwidgetqtabbar

How to prevent tab buttons from shrinking in QTabWidget


How do I prevent the tab buttons of a Qt5 QTabWidget from shrinking, thus obscuring the full tab names, when I shrink the containing window?

Here's a self-contained example of the problem:

#include <QApplication>
#include <QMainWindow>
#include <QtGui>
#include <QTableWidget>

QTableWidget*
makeTableWidget( QWidget* parent )
{
    QTableWidget* tableWidget = new QTableWidget( 20, 20, parent );

    for( int irow = 0; irow < 20; irow++ ) {
      for( int icol = 0; icol < 20; icol++ ) {
            QTableWidgetItem* newItem = new QTableWidgetItem( QString( "%1,%2" ).arg( irow ).arg( icol ) );
            tableWidget->setItem( irow, icol, newItem );
      }
    }

    return tableWidget;
}

int
main( int argc, char *argv[] ) {
    QApplication app( argc, argv );

    QMainWindow* mw = new QMainWindow();

    QTabWidget* tabs = new QTabWidget();

    tabs->setUsesScrollButtons( true );

    for( int itab = 0; itab < 10; itab++ ) {
            QTableWidget* tableWidget = makeTableWidget( mw );
            tabs->addTab( tableWidget, QString( "Table%1" ).arg( itab, 2, 10, QLatin1Char( '0' ) ) );
    }

    mw->setCentralWidget( tabs );

    mw->show();

    return app.exec();
}

When the main window is big enough, I can see the full names of each tab:

enter image description here

When I shrink the main window, however, the tab names get shortened with part of each name elided, even though I have enabled scrolling for the tab bar:

enter image description here

Since the tab-bar scrolling is enabled, it seems like it would work from a UI perspective to keep the tab-names at full size, so the user would be able to read each one unambiguously.

I need to know, however, how to modify the above code so that the tab button names do not shrink and thus get partially elided.

For the sake of brevity I'll spare description of my many misguided newbie experiments trying to figure this out.

Thanks


Solution

  • You can disable text elision with:

    tabs->setElideMode(Qt::ElideNone);