1

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!
chacham15
  • 13,719
  • 26
  • 104
  • 207
  • 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." – mip Dec 21 '12 at 20:02
  • 1
    @doc the problem is with: "where necessary." This is wrapping before it is necessary. – chacham15 Dec 21 '12 at 21:09
  • 1
    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. – mip Dec 21 '12 at 21:56
  • @doc I think that that is the answer. – chacham15 Dec 22 '12 at 00:55

1 Answers1

0

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 
fredcrs
  • 3,558
  • 7
  • 33
  • 55
  • The problem is with/without a layout (I have tried both) . You are correct, however, that if I set the height manually then it will fit to one line but I also have the following problem: http://stackoverflow.com/questions/13994902/how-do-i-get-a-qlabel-to-expand-to-full-width#13994902 – chacham15 Dec 21 '12 at 19:00