4

I have a Qt application where I have a textedit and a label. When a user presses the button, the textedit text should be displayed on label. For the label I have set few properties like word wrap is enabled and horizontal and vertically it is aligned center. Below is the screenshot :

enter image description here

Now I have to automatically adjust the size of the text in label so that if someone enters a large string, then it should fit inside the label, that means size of text should decrease. And if the text string is small, then size should increase automatically to fill up the complete label. Currently if I am typing it the large string, it looks like something:

enter image description here

As you can see, in the above image, text is moving out of the label. It should remain inside the label.

How to detect in application if the text is moving out of the label height & width. Then how to reduce the text size. I want the size to automatically increase if the string is small and decrease it string is large to fill up the complete label. Is there any class or something provided in QT. Any help or example please. Thanks.

EDIT: With the below code I am able to reduce the size of text to fit inside the label width but not able to make the text multi line.

QString string = ui->textEdit->toPlainText();   //Getting data from textEdit

ui->label->setAlignment(Qt::AlignCenter);   //Aligning label text to center
QFont f("Arial",50);        //Setting the default font size to 50
QFontMetrics fm(f);
ui->label->setFont(f);      //Setting the font to the label
int width = fm.width(string);   //Getting the width of the string
int size;
while(width >= 870)     //870 is the max width of label
{

    size = ui->label->font().pointSize()-1;     //Reduce font size by 1
    QFont newFont("Arial",size);            
    QFontMetrics nfm(newFont);          
    ui->label->setFont(newFont);        //Set the new font with new size
    width = nfm.width(string);      //Get the new width
}
ui->label->setText(string);
S Andrew
  • 4,456
  • 11
  • 57
  • 126
  • This can become tricky because height and width are not independent to each other. However, you should start with [`QFontMetrics`](http://doc.qt.io/qt-5/qfontmetrics.html). It provides font/text size calculations. The rest is surely some kind of iterations until the calculated bounds are somehow sufficient. – Scheff's Cat Mar 07 '17 at 16:17
  • @Scheff Thanks for the QFontMetrics. I will read its documentation. Can you provide any similar matching example of auto adjusting the text. – S Andrew Mar 07 '17 at 16:21
  • Not (yet). Until now, I only used the font of a widget and simply measured the bounds of string to layout cells of a table (which where painted then). There was no line wrapping necessary. – Scheff's Cat Mar 07 '17 at 16:28
  • @Scheff May be that could give me a more idea of how to do it. Can you post your example. Thanks – S Andrew Mar 07 '17 at 16:30
  • Did you consider `QFont::setPointSizeF()`? – Scheff's Cat Mar 07 '17 at 16:30
  • Also `minimumSizeHint()` could help. I think it does reflect changes in the text so that it can be used as an indicator of the need to resize the font. – Emerald Weapon Mar 07 '17 at 16:31
  • @SAndrew The whole code I cannot post. (It's not self-standing and too large.) The relevant part is: `QFontMetrics fontMetrics = this->fontMetrics(); QRect rect = fontMetrics.boundingRect(QString::fromUtf8(get(i, j).c_str()));` where `get(i, j)` provides a string with text of cell[i,j]. – Scheff's Cat Mar 07 '17 at 16:32
  • @EmeraldWeapon Thanks for the sizeHint(). I will read its documentation. – S Andrew Mar 07 '17 at 16:34
  • @Scheff Thanks for the code snippet. – S Andrew Mar 07 '17 at 16:35
  • @SAndrew I forgot to mention that the `fontMetrics` is determined once, not for every cell. (I'm not sure how expansive the call is.) But, surely not an issue in your case because you have only one text not a matrix of texts... – Scheff's Cat Mar 07 '17 at 16:37
  • @Scheff Please see the edit in my question. It automatically reduce the font size and then show it on label. But the problem is text appears only in one line. But I want it to be in mulitple lines so that it will more large and clear. What I am planning is to set the minimum size policy and enable the word wrap. Then I think the size policy will not allow the width to reduce till a point and then it will automatically get into next line – S Andrew Mar 08 '17 at 09:10
  • @Scheff As I am new to this size policy. Can you help me with this.? – S Andrew Mar 08 '17 at 09:11
  • @SAndrew You have to use a wrap mode. Consider [`QFontMetrix::boundingRect(const QRect &rect, int flags, ...`)](http://doc.qt.io/qt-5/qfontmetrics.html#boundingRect-2). You can define the bound rectangle `rect`(width like in `QLabel`, height eventually to a very large value) and define `Qt::TextWordWrap` in the `flags`. Btw. I found this link: [For Qt 4.6.x, how to auto-size text to fit in a specified width?](http://www.qtcentre.org/threads/27839-For-Qt-4-6-x-how-to-auto-size-text-to-fit-in-a-specified-width). Unfortunately, I have currently not enough time to make a sample and check this out. – Scheff's Cat Mar 08 '17 at 10:37
  • [This answer](http://stackoverflow.com/a/36577062/1329652) might be a good starting point, and see also [this one](http://stackoverflow.com/a/40874113/1329652) for labels in a layout. – Kuba hasn't forgotten Monica Mar 08 '17 at 23:20
  • @Scheff Please check my answer.I somehow managed to do it. It works most of the times. It has disadvantage that it didn't work for the string whose width is less than the width of label. I am working on it. I didn't get the time to check for boundingRect(). Meanwhile can you please tell me how can I use it my example. Thanks – S Andrew Mar 09 '17 at 05:38
  • @KubaOber Thanks for the links. I will read them. Meanwhile can you check the code I posted in my answer. – S Andrew Mar 09 '17 at 05:38

3 Answers3

8

You (S. Andrew) solved it a little bit different like I proposed (just a statement but not critics). You did the word wrapping by yourself.

I wrote a minimal complete application to check how the Qt internal word wrapping can be used for your problem:

// standard C++ header:
#include <iostream>
#include <string>

// Qt header:
#include <QApplication>
#include <QBoxLayout>
#include <QFrame>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QMainWindow>
#include <QStyle>

using namespace std;

class Label: public QLabel {

  public:
    void layout();
    QRect documentRect(); // borrowed from QLabelPrivate
  protected:
    virtual void resizeEvent(QResizeEvent *pQEvent);
};

QRect Label::documentRect()
{
  QRect rect = contentsRect();
  int m = margin(); rect.adjust(m, m, -m, -m);
  layoutDirection();
  const int align
    = QStyle::visualAlignment(layoutDirection(), QLabel::alignment());
  int i = indent();
  if (i < 0 && frameWidth()) { // no indent, but we do have a frame
    m = fontMetrics().width(QLatin1Char('x')) / 2 - m;
  }
  if (m > 0) {
    if (align & Qt::AlignLeft) rect.setLeft(rect.left() + m);
    if (align & Qt::AlignRight) rect.setRight(rect.right() - m);
    if (align & Qt::AlignTop) rect.setTop(rect.top() + m);
    if (align & Qt::AlignBottom) rect.setBottom(rect.bottom() - m);
  }
  return rect;
}

void Label::layout()
{
  // get initial settings
  QString text = this->text();
  QRect rectLbl = documentRect(); // wrong: contentsRect();
  QFont font = this->font();
  int size = font.pointSize();
  QFontMetrics fontMetrics(font);
  QRect rect = fontMetrics.boundingRect(rectLbl,
    Qt::TextWordWrap, text);
  // decide whether to increase or decrease
  int step = rect.height() > rectLbl.height() ? -1 : 1;
  // iterate until text fits best into rectangle of label
  for (;;) {
    font.setPointSize(size + step);
    QFontMetrics fontMetrics(font);
    rect = fontMetrics.boundingRect(rectLbl,
      Qt::TextWordWrap, text);
    if (size <= 1) {
      cout << "Font cannot be made smaller!" << endl;
      break;
    }
    if (step < 0) {
      size += step;
      if (rect.height() < rectLbl.height()) break;
    } else {
      if (rect.height() > rectLbl.height()) break;
      size += step;
    }
  }
  // apply result of iteration
  font.setPointSize(size);
  setFont(font);
}

void Label::resizeEvent(QResizeEvent *pQEvent)
{
  QLabel::resizeEvent(pQEvent);
  layout();
}

int main(int argc, char **argv)
{
  cout << QT_VERSION_STR << endl;
  // main application
#undef qApp // undef macro qApp out of the way
  QApplication qApp(argc, argv);
  // setup GUI
  QMainWindow qWin;
  QGroupBox qGBox;
  QVBoxLayout qBox;
  Label qLbl;
  qLbl.setFrameStyle(Label::Box);
  qLbl.setFrameShadow(Label::Sunken);
  qLbl.setWordWrap(true);
  qBox.addWidget(&qLbl, 1);
  QLineEdit qTxt;
  qBox.addWidget(&qTxt, 0);
  qGBox.setLayout(&qBox);
  qWin.setCentralWidget(&qGBox);
  qWin.show();
  // install signal handlers
  QObject::connect(&qTxt, &QLineEdit::editingFinished,
    [&qTxt, &qLbl]() {
      QString text = qTxt.text();
      qLbl.setText(text);
      qLbl.layout();
    });
  return qApp.exec();
}

Compiled and tested with VS2013 / Qt 5.6 on Windows 10 (64 bit):

Snapshot of testQFontMetric.exe

When playing around with this test application, I recognized that the text fits not everytimes perfectly into the QLabel. I tried to improve the code exchanging QRect rectLbl = rect(); with QRect rectLbl = contentsRect();. This made it better but still not perfect. It seems there is some finetuning necessary (where the development starts to become effort). (See update at end of text.)

Actually, it would not be necessary to derive QLabel. In my first implementation, layout() was a function with QLabel& and const QString& as parameters.

After I got the font size management working, I intended to consider resize events also. Googling a little bit, I found the solution to apply event filters. However, event filters are called before the event is processed but I need after. Finally, I decided to inherit QLabel and to overload QLabel::resizeEvent() to keep things simple.

Btw. I noticed it is even not necessary to set

height eventually to a very large value

as I suggested in a comment earlier. It seems that QFontMetrics::boundingRect(const QRect &rect, int flags, ...) increases the height automa[gt]ically to keep required width when Qt::TextWordWrap is enabled.

Update:

@annacarolina encouraged me to investigate a little bit deeper into this issue that font size is sometimes choosen to large. Some debugging in Label::layout() uncovered that sometimes computed rect looked like unwrapped text where visual output was wrapped. This made me suspiciuous about correctness of the rectLbl. Thus, I started in qlabel.cpp on woboq.org but actually the Qt forum QLabel: Resize font to contentsRect provided the final hint which leaded me to QLabelPrivate::documentRect() (actually again on woboq.org where I already had looked for enlightment). Thus, I added a method Label::documentRect() to my class. This makes results much better (although I'm not fully convinced about "perfect").

Scheff's Cat
  • 16,517
  • 5
  • 25
  • 45
  • Thanks for this good example. I am also facing simillar issue. Can you explain your code a little bit. I think the main logic is inside this `void Label::layout()`. Can you explain it. Thanks :) – anna carolina Mar 30 '17 at 10:32
  • `QRect rectLbl = contentsRect();` in this I am getting `400x300` but actually my window is full screen so label is also full screen. It should return `1366x768`. How to do it – anna carolina Mar 30 '17 at 10:57
  • @annacarolina I just tested my app again (on Windows 10) and made it full-screen. The text was re-layouted and increased, appropriately. In your case, I suspect there could be trouble concerning `resizeEvent()`. In [another sample](http://stackoverflow.com/questions/42833511/qt-how-to-create-image-that-scale-with-window-and-keeps-aspect-ratio/42852010#42852010), I observed similar issues which I could fix by a work-around - applying a frame to the label. (The frame may force a sufficient re-layout on every size change what's needed there and here.) – Scheff's Cat Mar 30 '17 at 14:06
  • I somehow managed to do it but it increases more than its resolution. My current height and width is 1366x768. So the width remains less than 1366 but the height goes more than 768. Is there any way to reduce the height too – anna carolina Mar 31 '17 at 04:17
  • @annacarolina I believe I fixed it. Source code is updated and there is a note about this at end of my answer. – Scheff's Cat Mar 31 '17 at 06:16
  • Yes. I set the height value manually. So now it seems to work. I will test it with more test cases later. Thanks.! – anna carolina Mar 31 '17 at 06:30
  • Thanks for the solution, @Scheff. I just had to add a check for `rect.width()` in `Label::layout()`, to correct the situations when height already fits, but width doesn't. Also, I was surprised, that `QLabel::setFont()` doesn't work when the label's font is set in Qt stylesheet, even for parent widgets, when the label just inherits the font size from its parent hierarchy. So one has to make sure that there is no `font-size` rule in the stylesheet which would be inherited by the target `Label`. – Michal Fapso Mar 03 '19 at 20:38
  • @MichalFapso I must admit that I've no experience with styles in Qt. For what I'm doing usually, the default look&feel is mostly sufficient. – Scheff's Cat Mar 04 '19 at 06:13
  • @MichalFapso Your note about `rect.width()`, I didn't understand. The algorithm is based on text floating. If the width isn't matching then the floating doesn't work correctly. That may happen in extreme cases (no wrapping can be applied for a line). OK, something I didn't consider... (Using another wrap mode would fix this but I assume that was not intended in your case.) – Scheff's Cat Mar 04 '19 at 06:20
  • 1
    You're right, @Scheff. Your code behaves correctly. Just in my case, my QLabel had a fixed size (minSize == maxSize) and for that reason I needed to add the check for width. Here is the code with screenshots: https://gist.github.com/michalfapso/f1ecac7e81dc986cb3bc941abe85d5e4 – Michal Fapso Mar 04 '19 at 20:41
  • Thank you. Excellent solution. – user1372617 Nov 27 '19 at 17:03
2

In the following code, I am making a logic where I am first getting all the words in the string. Then I am appending the words in QList<QString> data and checking if the width of the appended words is smaller than then width of the label. If the width goes above the width of label then I break it using \n. So in this way I made a list of the sub strings whose total width is around the width of the label and saved it in the List. Then I am calculating the width of sub strings stored in the list and then decreasing its font size till its total width is less than width of the label. After this I am displaying it on the label.

QList<QString> data;
CountWords Word;
ui->label->clear();
QString string = ui->textEdit->toPlainText();   //Getting data from textEdit
QStringList count = Word.GetWords(string);   //Here I get the list of words in string
ui->label->setAlignment(Qt::AlignCenter);   //Aligning label text to center
QFont f("Arial",50);        //Setting the default font size to 50
QFontMetrics fm(f);
ui->label->setFont(f);      //Setting the font to the label
int size,fontSize;
QString temp = ui->label->text();
int last = count.size();
//Saving the words in QList
for(int i=0;i<count.size();i++)
{
    temp.append(count[i]+" ");
    size = fm.width(temp);
    if(size > 870)
    {
        temp.append("\n");
        data << temp;
        //data.append(temp);
        temp.clear();
    }
    if((last-1)==i)
    {
         subString.append("\n");
         data << subString;
         subString.clear();
    }
}

 //decreasing the font size
QList<int> wide;
for(int i=0;i<data.size();i++)
{
    wide << fm.width(data[i]);
    while(wide[i] >= 870)
    {
        fontSize = ui->label->font().pointSize() - 1;
        QFont newFont("Arial",fontSize);
        QFontMetrics nfm(newFont);
        ui->label->setFont(newFont);
        wide[i] = 0;
        wide[i] = nfm.width(data[i]);
    }

}

//Finally displaying it on label
QString labelData;
for(int i=0;i<data.size();i++)
{
    labelData = ui->label->text();
    labelData.append(data[i]);
    ui->label->setText(labelData);

}
S Andrew
  • 4,456
  • 11
  • 57
  • 126
1

After struggling with this issue, I create DynamicFontSizeLabel and DynamicFontSizePushButton widgets. Hope it helps.

https://github.com/jonaias/DynamicFontSizeWidgets/

Thanks Scheff for some inspiration.

Jonaias
  • 345
  • 3
  • 9