Search code examples
qtword-wrap

Is there a way to enable word wrapping of text on some simple widgets like QPushButton?


I'd like to make QPushButton word wrap and expand it's height instead of expanding width. How can I do that?


Solution

  • I have solved the problem with wordwrap on a button this way:

    QPushButton *createQPushButtonWithWordWrap(QWidget *parent, const QString &text)
    {
       auto btn = new QPushButton(parent);
       auto label = new QLabel(text,btn);
       label->setWordWrap(true);
    
       auto layout = new QHBoxLayout(btn);
       layout->addWidget(label,0,Qt::AlignCenter);
    
       return btn;
    }