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. dynamically change qpushbuttons font size
Qt 6.11 is out! See what's new in the release blog

dynamically change qpushbuttons font size

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 3.9k Views 3 Watching
  • 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.
  • H Offline
    H Offline
    hobbyProgrammer
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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

      H 1 Reply Last reply
      6
      • mrjjM mrjj

        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

        H Offline
        H Offline
        hobbyProgrammer
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

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