Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QLabel word wrap mode

I have a label that sometimes contain a long text with no spaces (path in the computer).

So word-wrap wraps it very weirdly.

Is there a way to make the word-wrap of the label break in the middle of the word or not only at white spaces?

like image 382
sara Avatar asked Sep 04 '12 11:09

sara


People also ask

How do you wrap text in Qt?

Text Wrap at char instead of white space According to this answer(in op's comment) provided by @ekhumoro, if you are looking for wrapping a line based on comma, you can insert a zero-width-space after the char you want to wrap and use the built in word wrap function.

How do I turn on word wrap in word?

Go to Picture Format or Shape Format and select Arrange > Wrap Text. If the window is wide enough, Word displays Wrap Text directly on the Picture Format tab. Choose the wrapping options that you want to apply.

What is word wrap mode?

Word wrap may refer to any of the following: 1. Sometimes referred to as a run around and wrap around, word wrap is a feature in text editors and word processors. It moves the cursor to the next line when reaching the end without requiring you to press Enter .


2 Answers

This isn't elegant but does work...
So say header class has Private:

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

and of course defining thisLabel some where. so it would be nice if it was this simple....

thisLabel->setWordWrap(true);

that's fine IF AND ONLY IF the word has break points (WHICH PATHS SHOULD AVOID)

SO keep your actual path in a separate string if you need it for QFile purposes later. Then manually define a character per line number, and insert the spaces into the string.... so we'll say 50 chars 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.... simulated WordWrap with no original spaces...

just remember that pathName string is now just for pretty QLabel purposes and that the pathNameClean string is the actual path. Qt programs will crash if you try to open a file with a space injected path.....

(if there's no simple class method it's likely just a few lines of code to do... and why problem solving is a programmers best tool!)

like image 134
budda Avatar answered Sep 29 '22 12:09

budda


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

like image 33
Luca Carlon Avatar answered Sep 29 '22 14:09

Luca Carlon