1
votes

In the following code there is clearly enough space for the label to fit on one line, but for some reason it splits it into two lines after 'thats'. Why and how do I prevent this?

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    this->setFixedSize(250,100);

    QLabel *label = new QLabel;
    label->setStyleSheet("background-color:blue");
    label->setWordWrap(true);
    label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    label->setText("Oh my gosh thats too funny!");
    label->setParent(this);

}

Again for clarity, it shows:

Oh my gosh thats 
too funny!

And I want:

Oh my gosh thats too funny!
1
Shouldn't it be label->setWordWrap(false);??? If it's not a typo then I think you have an answer. From Qt docs: "If this property is true then label text is wrapped where necessary at word-breaks; otherwise it is not wrapped at all."doc
@doc the problem is with: "where necessary." This is wrapping before it is necessary.chacham15
If you want your QLabel to fit the size of parent widget you should use layouts. QVBoxLayout * layout = new QVBoxLayout; layout->addWidget(label); this->setLayout(layout);. The QWidget does not reposition and resize its child widgets by itself.doc
@doc I think that that is the answer.chacham15

1 Answers

0
votes

Are you using any layout in the Widget ? If not try setting QLabels width and height manually

EDIT:

I wrote a code that was not using any QLayout, it works fine, quite simple

QLabel *label= new QLabel(QString::fromUtf8("Client code"), this);
label->setGeometry(posx, posy, w, h);

hope this helps

PS: 'this' is my Dialog

class MyDialog : public QDialog