PDA

View Full Version : QLabel Word Wrapping adds unnecessary line breaks



forgottenduck
30th March 2015, 17:08
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:



void PLabel::resizeEvent(QResizeEvent *event){
QLabel::resizeEvent( event );
if ( wordWrap() && sizePolicy().verticalPolicy() == QSizePolicy::Minimum ) {
setMinimumHeight(sizeHint().height());
}
}


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:

11042

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?

wysota
31st March 2015, 09:10
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.

forgottenduck
1st April 2015, 22:17
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 (https://bugreports.qt.io/browse/QTBUG-7103) seems to be a similar problem, the effect is the same anyway even though I am not setting a fixed width. This one (http://www.qtforum.org/article/15522/word-wrapping-qlabel-in-a-layout-qt3-x-solved.html) 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:



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
#include <QScrollArea>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QWidget* panelContent = new QWidget;

QGridLayout* grid = new QGridLayout;
grid->setAlignment(Qt::AlignTop);
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.");
label1->setSizePolicy(label1->sizePolicy().horizontalPolicy(), QSizePolicy::Fixed);
label1->setWordWrap(true);
QLabel* label2 = new QLabel("This is a short label.");
label2->setWordWrap(true);
grid->addWidget(label1, 0, 0);
grid->addWidget(new QLineEdit, 0, 1);
grid->addWidget(label2, 1, 0);
grid->addWidget(new QLineEdit, 1, 1);
panelContent->setLayout(grid);

setCentralWidget(panelContent);

}

MainWindow::~MainWindow()
{
delete ui;
}


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.



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.



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.

wysota
1st April 2015, 22:42
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.

forgottenduck
1st April 2015, 23:02
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.

wysota
1st April 2015, 23:21
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.

forgottenduck
2nd April 2015, 16:24
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!