Search code examples
qtqwidgetqtabwidget

wrap text in the tabBar of QTabWidget


Im changing text of QTabWidget dynamically and also changing its font family. so the text in the tabBar header is exceeding the actual width. how i can control the text to wrap inside the tabBar width like

"Text.."

im setting the font family using stylesheet

QString menuBarStyle = QString("QTabBar::tab  {font-family: %1;font-style: %2;font-size: 11pt;font-weight: %3;width: 130px}").arg(m_currentFont.family()).arg(fontStyle).arg(fontWeight);
   TabWidget->setStyleSheet(menuBarStyle);

Solution

  • Qt stylesheet technique may or may not let to solve it that but I once resolved similar problem like that:

    QString elidedText(const QFont& someFont, int width)
    {
        QFontMetrics metrix( someFont );
        return metrix.elidedText(text, Qt::ElideRight, width);
    }
    

    So, in addition to font object you have to have the width to fit the text in. Also frequently applying changes via the stylesheet for the widget can hardly be optimal solution, it does a bit too much to get the small change.