QLabel word wrap mode

I have a shortcut that sometimes contains long text with no spaces (path on the computer).

So the word wrap is very strange.

Is there a way to make word wrapping with a gap in the middle of the word or not just in empty places?

+9
qt label word-wrap
source share
2 answers

This is not elegant, but it works ... So, the header class has Private:

QLabel *thisLabel; QString *pathName; QString *pathNameClean; 

and, of course, defining this label somewhere. so it would be nice if it were that simple ....

 thisLabel->setWordWrap(true); 

that the penalty is IF and ONLY if the word has break points (WHICH WAY TO AVOID)

SO will save your actual path on a separate line if you need it later for QFile. Then manually define the character on the line number and insert spaces in the line .... so we say that 50 characters is a good width ...

  pathNameClean = new QString(pathName); int c = pathName->length(); if( c > 50) { for(int i = 1; i <= c/50; i++) { int n = i * 50; pathName->insert(n, " "); } } thisLabel->setText(pathName); 

Shazam .... imitated WordWrap without original spaces ...

just remember that the pathName line is now just for good QLabel purposes and that the pathNameClean line is the actual path. Qt programs will crash if you try to open the file using a space entered in the path .....

(if there is no simple class method, probably just a few lines of code ... and why problem solving is the best tool for programmers!)

+5
source share

One way is to use the QTextOption class with QTextDocument instead of QLabel. This will allow you to use QTextOption :: WrapMode . QTextOption :: WrapAtWordBoundaryOrAnywhere should do what you want.

+4
source share

All Articles