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

dynamically change qpushbuttons font size



  • I have a grid of pushbuttons, and the size of the buttons vary depending on the size of the window.
    I would like to change the font-size dynamiccaly depending on the buttons size.

    Does anyone know how to do that?


  • Lifetime Qt Champion

    Hi
    Do you mean to make text as large as will fit in button ?

    Unless you also scale the height of the buttons (normally fixed)
    there won't be much scaling possible.

    However, if you really got a bigger area (width and height) then you can use
    https://doc.qt.io/qt-5/qfontmetrics.html#details
    To know the size of the text and then change the font size and recheck
    if it can fit. If not use the point size before.

    Update: Some very fast made code to play with.
    Disclaimer. Will have bugs for corner cases.

    void scalerFontSizeToFitBB(QPushButton *Button)
    {
        qreal oldFontSize, newFontSize;
    
        oldFontSize = Button->font().pointSizeF();
        QFont font;
    
        int h = Button->rect().height() - 8;
        int w = Button->rect().width() - 8;
        qreal step = 0.5;    
        for (int i = 0 ; i < 25 ; i++) {
            QRect textRect = Button->fontMetrics().boundingRect(Button->text());
            if ( textRect.height() < h  && textRect.width() <  w ) {
                newFontSize = oldFontSize += step;
                font.setPointSizeF(newFontSize);
                Button->setFont(font);
                oldFontSize = newFontSize;
                qDebug() << i;
            } else continue; // skip rest
        }
    
        // set result
        oldFontSize -= step; // use the size before we didnt fit
        font.setPointSizeF(oldFontSize);
        Button->setFont(font);
    }
    
    
    void MainWindow::ButtonClicked()
    {
        // scale all buttons
        QList<QPushButton *> buttonList = findChildren<QPushButton *>();
        for (QPushButton  *button : buttonList) {
            scalerFontSizeToFitBB(button);
        }
    }
    

    And do not put in unmodified in
    void MainWindow::resizeEvent(QResizeEvent *event)
    as it might lead to infinite recursion and crash.

    Fast test
    alt text


  • Lifetime Qt Champion

    Hi
    Do you mean to make text as large as will fit in button ?

    Unless you also scale the height of the buttons (normally fixed)
    there won't be much scaling possible.

    However, if you really got a bigger area (width and height) then you can use
    https://doc.qt.io/qt-5/qfontmetrics.html#details
    To know the size of the text and then change the font size and recheck
    if it can fit. If not use the point size before.

    Update: Some very fast made code to play with.
    Disclaimer. Will have bugs for corner cases.

    void scalerFontSizeToFitBB(QPushButton *Button)
    {
        qreal oldFontSize, newFontSize;
    
        oldFontSize = Button->font().pointSizeF();
        QFont font;
    
        int h = Button->rect().height() - 8;
        int w = Button->rect().width() - 8;
        qreal step = 0.5;    
        for (int i = 0 ; i < 25 ; i++) {
            QRect textRect = Button->fontMetrics().boundingRect(Button->text());
            if ( textRect.height() < h  && textRect.width() <  w ) {
                newFontSize = oldFontSize += step;
                font.setPointSizeF(newFontSize);
                Button->setFont(font);
                oldFontSize = newFontSize;
                qDebug() << i;
            } else continue; // skip rest
        }
    
        // set result
        oldFontSize -= step; // use the size before we didnt fit
        font.setPointSizeF(oldFontSize);
        Button->setFont(font);
    }
    
    
    void MainWindow::ButtonClicked()
    {
        // scale all buttons
        QList<QPushButton *> buttonList = findChildren<QPushButton *>();
        for (QPushButton  *button : buttonList) {
            scalerFontSizeToFitBB(button);
        }
    }
    

    And do not put in unmodified in
    void MainWindow::resizeEvent(QResizeEvent *event)
    as it might lead to infinite recursion and crash.

    Fast test
    alt text



  • @mrjj that looks like what I need. Thank you so much.


Log in to reply