PDA

View Full Version : WordWrap policy for QLabel



anju123
4th July 2007, 13:46
I need to set the wordwrap policy of Qlabel so that it wraps the text anywhere and not just the word-breaks. I have read that the same can be done in QTextEdit. Is this thing possible with QLabel?

jpn
4th July 2007, 15:13
I have a feeling that it might be easier to write a custom label widget with the help of QTextLayout than to hack QLabel for this.

anju123
1st August 2007, 13:12
I am again back on the same problem..
Somehow I am not able to figure out how to use the QTextLayout along with QLabel so that the text on the label is displayed according to the set WordWrap policy..
thanks in advance for any kind of help

jpn
17th August 2007, 21:52
Perhaps you could simply use QTextEdit?


class TextEdit : public QTextEdit
{
Q_OBJECT

public:
TextEdit(const QString& text, QWidget* parent = 0)
: QTextEdit(text, parent)
{
// make it look and feel like an ordinary label
setReadOnly(true);
setFrameStyle(QFrame::NoFrame);
QPalette pal = palette();
pal.setColor(QPalette::Base, Qt::transparent);
setPalette(pal);

// wrap anywhere, adjust minimum height on the fly
setLineWrapMode(QTextEdit::WidgetWidth);
setWordWrapMode(QTextOption::WrapAnywhere);
connect(document()->documentLayout(),
SIGNAL(documentSizeChanged(QSizeF)),
this, SLOT(adjustMinimumSize(QSizeF)));
}

private slots:
void adjustMinimumSize(const QSizeF& size)
{
setMinimumHeight(size.height() + 2 * frameWidth());
}
};