Results 1 to 7 of 7

Thread: QLabel Word Wrapping adds unnecessary line breaks

  1. #1
    Join Date
    Sep 2014
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default QLabel Word Wrapping adds unnecessary line breaks

    I've had a few issues with word wrapping in my current project. Basically I have a bunch of labels with input widgets in a QGridLayout, and some of the descriptive labels are pretty verbose. So I enabled word wrap so that my labels column wouldn't take up too much space in the layout and force my input widgets to be extra small. There is a known issue which caused my labels to not set the correct height and some of the text would be cut off. For anyone that's interested in that solution, I managed to solve it with this snippet in my label subclass:

    Qt Code:
    1. void PLabel::resizeEvent(QResizeEvent *event){
    2. QLabel::resizeEvent( event );
    3. if ( wordWrap() && sizePolicy().verticalPolicy() == QSizePolicy::Minimum ) {
    4. setMinimumHeight(sizeHint().height());
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

    Note: the vertical policy must be minimum.

    Now my issue is that the labels are wrapping more than necessary in some cases, and I can't figure out why. For example:

    wordwrap_issue.png

    You can see here that the first 3 labels are fine, but the last several all take up 2 or more lines when they could easily fit on one. Can anyone clue me in as to what's happening behind the scenes here? Is there some method I can re-implement to control where it wraps?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLabel Word Wrapping adds unnecessary line breaks

    Quote Originally Posted by forgottenduck View Post
    I've had a few issues with word wrapping in my current project. Basically I have a bunch of labels with input widgets in a QGridLayout, and some of the descriptive labels are pretty verbose.
    You should keep the label minimal and provide a verbose description using tool tips.

    There is a known issue which caused my labels to not set the correct height and some of the text would be cut off.
    What issue is that?

    Note: the vertical policy must be minimum.
    The policy should rather be Fixed.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2014
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QLabel Word Wrapping adds unnecessary line breaks

    Quote Originally Posted by wysota View Post
    What issue is that?
    First off, thanks for the reply, your knowledge is always helpful on these forums. Calling it a known issue might be a stretch, I suppose it's more of a common problem without a great solution. Basically the size hint for QLabel does not return a good height in certain circumstances which cuts off part of the label, and there doesn't seem to be a good way to force it to not get cut off. I'm having trouble finding the original threads and bug reports I read the other day, but this bug report seems to be a similar problem, the effect is the same anyway even though I am not setting a fixed width. This one also seems similar, but I'm not a member there so I can't open the images (you're actually in that thread as well). I was able to reproduce the effect with this code for a mainWindow:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QGridLayout>
    4. #include <QLabel>
    5. #include <QLineEdit>
    6. #include <QScrollArea>
    7.  
    8. MainWindow::MainWindow(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::MainWindow)
    11. {
    12. ui->setupUi(this);
    13.  
    14. QWidget* panelContent = new QWidget;
    15.  
    16. QGridLayout* grid = new QGridLayout;
    17. grid->setAlignment(Qt::AlignTop);
    18. QLabel* label1 = new QLabel("This is a very long label with lots of words that might get cut off with word wrap. Hopefully I have made the label long enough to demonstrate this effect. Maybe I'll ad a few more words.");
    19. label1->setSizePolicy(label1->sizePolicy().horizontalPolicy(), QSizePolicy::Fixed);
    20. label1->setWordWrap(true);
    21. QLabel* label2 = new QLabel("This is a short label.");
    22. label2->setWordWrap(true);
    23. grid->addWidget(label1, 0, 0);
    24. grid->addWidget(new QLineEdit, 0, 1);
    25. grid->addWidget(label2, 1, 0);
    26. grid->addWidget(new QLineEdit, 1, 1);
    27. panelContent->setLayout(grid);
    28.  
    29. setCentralWidget(panelContent);
    30.  
    31. }
    32.  
    33. MainWindow::~MainWindow()
    34. {
    35. delete ui;
    36. }
    To copy to clipboard, switch view to plain text mode 

    If you resize the window you can see how the label gets cut off at certain sizes. I realize that this isn't exactly a bug because in most circumstances it would be acceptable for your labels to get cut off at such a small size, but in my particular application I was running into the sizing problems at much more reasonable window sizes. So setting the minimum height based on the size of the label (rather than applying the same minimum height to all my labels) seems to be an appropriate solution for me.

    Quote Originally Posted by wysota View Post
    You should keep the label minimal and provide a verbose description using tool tips.
    Unfortunately the label text is defined by the user (doing some dynamic form generation) so I need to be able to accommodate labels of any size.

    Quote Originally Posted by wysota View Post
    The policy should rather be Fixed.
    Does it really matter? It seems to behave the same either way, although I guess I don't want it to take up any extra space, so maybe fixed is more appropriate. In either case my labels get cut off without setting that minimum height during resize events.


    I'm okay with the sizing solution I currently have, but I really just don't understand why it seems to be word wrapping labels that don't need to be wrapped, and I end up with unnecessary empty space between my input widgets and my labels.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLabel Word Wrapping adds unnecessary line breaks

    Quote Originally Posted by forgottenduck View Post
    Does it really matter?
    Yes, because with Minimum the widget may become larger which in turn might leave gaps if any other widget with Minimum policy is placed below or above the label.

    I'm okay with the sizing solution I currently have, but I really just don't understand why it seems to be word wrapping labels that don't need to be wrapped, and I end up with unnecessary empty space between my input widgets and my labels.
    I would suspect some kind of rounding error or a similar off-by-one behavior.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2014
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QLabel Word Wrapping adds unnecessary line breaks

    Quote Originally Posted by wysota View Post
    I would suspect some kind of rounding error or a similar off-by-one behavior.
    Do you have any idea if there is some method I could reimplement in my QLabel subclass to control this behavior? I just really don't like the look of those unnecessary line breaks. I suppose I could just set my own line breaks based on the length of the string, which I assume is what QLabel does anyway, but I would have the advantage of programming for my one specific instance where I don't even need the labels to resize after they have been created.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QLabel Word Wrapping adds unnecessary line breaks

    Quote Originally Posted by forgottenduck View Post
    Do you have any idea if there is some method I could reimplement in my QLabel subclass to control this behavior?
    sizeHint(), I guess.

    I suppose I could just set my own line breaks based on the length of the string, which I assume is what QLabel does anyway,
    No, it doesn't You can use QTextLayout if you want to layout the text yourself.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    forgottenduck (2nd April 2015)

  8. #7
    Join Date
    Sep 2014
    Posts
    27
    Thanks
    2
    Thanked 2 Times in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QLabel Word Wrapping adds unnecessary line breaks

    Quote Originally Posted by wysota View Post
    You can use QTextLayout if you want to layout the text yourself.
    Ah this looks like it might offer what I need. I'll probably have to bookmark it and get back to the issue when I have more time, but it's good to know about. Thanks!

Similar Threads

  1. QTextTable and word wrapping
    By ssample in forum Qt Programming
    Replies: 0
    Last Post: 21st May 2013, 10:38
  2. qtexttable - word wrapping
    By ssample in forum General Programming
    Replies: 0
    Last Post: 19th May 2013, 09:55
  3. Word wrapping in a QTableWidget cell
    By jcooperddtd in forum Qt Programming
    Replies: 3
    Last Post: 1st May 2007, 04:57
  4. Word wrapping
    By bruccutler in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2007, 17:33
  5. word wrapping in QTable Cell
    By joseph in forum General Discussion
    Replies: 0
    Last Post: 27th November 2006, 10:30

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.