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. Resizing Qlabel text upon resizing window

Resizing Qlabel text upon resizing window

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 8.9k Views
  • 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.
  • W Offline
    W Offline
    whatisjeff
    wrote on last edited by
    #1

    So I know that the Qlabel widget itself will decide its size based on the layout its placed in. But its text does not do that, staying the same font size initially given to it. How would one change its font size whenever the application window is resized?

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

      Hi,

      You can use the resize event to alter the font size using QFontMetrics to find at what font size your text stays in the rectangle of the label.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • W Offline
        W Offline
        whatisjeff
        wrote on last edited by
        #3

        Thanks for the reply! Can you be more specific though? Which widget has the resize event? Ideally I'd like there to be a resize signal sent by the MainWindow that I can connect to a function that resizes QLabel. I haven't found anything similar though

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Subclass QLabel and reimplement the resizeEvent method.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          W 1 Reply Last reply
          0
          • SGaistS SGaist

            Subclass QLabel and reimplement the resizeEvent method.

            W Offline
            W Offline
            whatisjeff
            wrote on last edited by
            #5

            @SGaist Thanks again, that led me further down the path to solving the problem. Now I'm at another hurdle.

            So far I've re-implemented resizeEvent for the MainWindow and had it emit a signal

            MainWindow.h:
            ...
            void resizeEvent(QResizeEvent*);
            signals:
            void windowResized(int);

            MainWindow.cpp:
            void MainWindow::resizeEvent(QResizeEvent* event)
            {
            QMainWindow::resizeEvent(event);

            emit windowResized(this->width());
            

            }

            THEN I subclassed QLabel and created a slot to catch the signal and resize itself

            MyLabel.h:
            protected slots:
            void resizeLabel(int size);

            MyLabel.cpp:
            void MyLabel::resizeLabel(int size)
            {
            QFont font;
            font.setPointSize(size/26);
            this->setFont(font);
            }

            After which I connected the two in MainWindow.cpp:
            connect(this, SIGNAL(windowResized(int)), ui->label, SLOT(resizeLabel(int));

            HOWEVER this doesn't seem to resize the QLabel when making the MainWindow bigger/smaller. It remains a static font size. When debugging, I see that the point size for the font is indeed changing but it doesn't seem to be translating to the label.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by SGaist
              #6

              Your resizeLabel function does not in fact do anything on the widget size, it just modifies the font size

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              W 1 Reply Last reply
              0
              • SGaistS SGaist

                Your resizeLabel function does not in fact do anything on the widget size, it just modifies the font size

                W Offline
                W Offline
                whatisjeff
                wrote on last edited by
                #7

                @SGaist That is exactly what I want though. The widget should just resize itself based on the layout its placed in, right? But this alone does not increase its font size

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  whatisjeff
                  wrote on last edited by
                  #8

                  I believe I've solved it! Instead of setting the font in resizeLabel for the MyLabel class, just set the stylesheet, as so:

                  void MyLabel::resizeLabelH1(int size)
                  {
                  this->setStyleSheet("QLabel"
                  "{"
                  "font-size: " + QString::number(size) + "pt;"
                  "font-weight:bold;"
                  "}");
                  }

                  HOWEVER, if you've set the font size of the QLabel in the ui form editor, then it will use that size over the size you use in the stylesheet. In other words, styles set in the HTML text take precedent over styles set in the stylesheet. So for this to work, the original ui form text has to be as plain as can be

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Are you sure your function is called ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    W 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Are you sure your function is called ?

                      W Offline
                      W Offline
                      whatisjeff
                      wrote on last edited by
                      #10

                      @SGaist Positive, set a breakpoint within the function and it's called every time I resize the window

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Which version are you currently using ? The QFont or stylesheet ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        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