Rafael Ueda
Rafael Ueda

Reputation: 1

Fit QLabel height without WordWrap

I need to do a manual word wrap because the native wordWrap from Qt splits in wrong places. I've already done the wrap to my text, but the content wasn't showing all the content. The QLabel was cutting the top and the bottom like the image:

Can I fit the Qlabel to the height of the text inside it without wordWrap?

Upvotes: 0

Views: 1194

Answers (2)

I write it as height scalable :

QLabel lbl;
int count = 0;
QString str = "";

// set lbl text 
lbl.setText("hfdsf\ncsad\nfsc\dajkjkjkjhhkdkca\n925");
str = lbl.text();

for(int i = 0;i < str.length();i++)
    if(str.at(i).cell() == '\n')
        count++;

// resize lbl (width and height)
lbl.resize(lbl.fontMetrics().width("this is the max-length line in qlabel")
         , lbl.fontMetrics().height() * (count + 1));

Notice : this work if you change QLable font face or size! just in height scalable (before every thing set your QLabel frameShape to BOX).

if you want do width scalable-content, you should do these steps :

  • read QLabel(lbl object) text as line to line
  • calculate every line length
  • select maximum of line length
  • use of QLabel::fontMetrics().width(QString str) for investigate str size in width

I hope this can help you...

Upvotes: 1

ymoreau
ymoreau

Reputation: 4036

QLabel should resize automatically to the needed height if you place it in the layout, it does not matter if wordWrap is enabled or not, see the doc : http://doc.qt.io/qt-5/layout.html

Upvotes: 0

Related Questions